Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions bindonce.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,75 @@
});
};

var repeat = function(binder){
var expression = binder.value;
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);

if (!match) {
throw Error('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
expression);
}

var lhs = match[1];
var rhs = match[2];
var aliasAs = match[3];

match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);

if (!match) {
throw Error('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
lhs);
}
var valueIdentifier = match[3] || match[1];
var keyIdentifier = match[2];

if (aliasAs && (!/^[$a-zA-Z_][$a-zA-Z0-9_]*$/.test(aliasAs) ||
/^(null|undefined|this|\$index|\$first|\$middle|\$last|\$even|\$odd|\$parent)$/.test(aliasAs))) {
throw Error('badident', "alias '{0}' is invalid --- must be a valid JS identifier which is not a reserved name.",
aliasAs);
}

var collection = binder.scope.$eval(rhs);
var index, length,
previousNode = $element[0], // node that cloned nodes should be inserted after
collectionLength,
key, value, // key/value of iteration
collectionKeys;

if (aliasAs) {
$scope[aliasAs] = collection;
}

collectionLength = collection.length;
if (angular.isArray(collection)) {
collectionKeys = collection;
} else {
// if object, extract keys, sort them and use to determine order of iteration over obj props
collectionKeys = [];
for (var itemKey in collection) {
if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') {
collectionKeys.push(itemKey);
}
}
collectionKeys.sort();
}

// we are not using forEach for perf reasons (trying to avoid #call)
for (index = 0; index < collectionLength; index++) {
key = (collection === collectionKeys) ? index : collectionKeys[index];
value = collection[key];
var scope = binder.scope.$new()
transclude(binder, scope);
scope[valueIdentifier] = value;
if (keyIdentifier) scope[keyIdentifier] = key;
scope.$index = index;
scope.$first = (index === 0);
scope.$last = (index === (collectionLength - 1));
scope.$middle = !(scope.$first || scope.$last);
scope.$odd = !(scope.$even = (index&1) === 0);
}
};

var ctrl =
{
watcherRemover: undefined,
Expand Down Expand Up @@ -139,6 +208,10 @@
{
var binder = this.binders.shift();
if (this.group && this.group != binder.group) continue;
if (binder.attr == 'boRepeat') {
repeat(binder)
continue;
}
var value = binder.scope.$eval((binder.interpolate) ? $interpolate(binder.value) : binder.value);
switch (binder.attr)
{
Expand Down Expand Up @@ -254,10 +327,12 @@
{ directiveName: 'boValue', attribute: 'value' },
{ directiveName: 'boAttr', attribute: 'attr' },

{ directiveName: 'boRepeat', transclude: 'element', terminal: true, priority: 1000, interpolate : false },
{ directiveName: 'boIf', transclude: 'element', terminal: true, priority: 1000 },
{ directiveName: 'boSwitch', require: 'boSwitch', controller: function () { this.cases = {}; } },
{ directiveName: 'boSwitchWhen', transclude: 'element', priority: 800, require: '^boSwitch' },
{ directiveName: 'boSwitchDefault', transclude: 'element', priority: 800, require: '^boSwitch' }

],
function (boDirective)
{
Expand Down
71 changes: 71 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Karma configuration
// Generated on Fri Jul 04 2014 17:55:25 GMT-0700 (PDT)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],


// list of files / patterns to load in the browser
files: [
'test/jquery/**/*.js',
'test/angularjs/**/*.js',
'test/angularjs-mocks/**/*.js',
'bindonce.js',
'test/*.js'
],


// list of files to exclude
exclude: [

],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {

},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
"components"
],
"dependencies": {
},
"devDependencies": {
"phantomjs" : "~1.9",
"karma": "~0.12",
"karma-chai": "~0.1",
"karma-mocha": "~0.1",
"karma-phantomjs-launcher": "^0.1.4",
"mocha": "~1.20"
},
"keywords": [
"angularjs",
Expand Down
Loading