Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
efe5354
commit 947f8e6
Showing
19 changed files
with
387 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
temp | ||
dist | ||
.DS_Store | ||
*.swp | ||
*.swo | ||
node_modules | ||
_SpecRunner.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*global module:false*/ | ||
module.exports = function(grunt) { | ||
|
||
grunt.loadNpmTasks('grunt-rigger'); | ||
|
||
// Project configuration. | ||
grunt.initConfig({ | ||
meta: { | ||
version: '0.0.5', | ||
banner: '// Backbone.ComputedFields, v<%= meta.version %>\n' + | ||
'// Copyright (c)<%= grunt.template.today("yyyy") %> alexander.beletsky@gmail.com\n' + | ||
'// Distributed under MIT license\n' + | ||
'// http://github.com/alexander.beletsky/backbone.clickdebounce' | ||
}, | ||
|
||
lint: { | ||
files: ['src/backbone.computedfields*.js'] | ||
}, | ||
|
||
rig: { | ||
build: { | ||
src: ['<banner:meta.banner>', 'src/backbone.computedfields.js'], | ||
dest: 'lib/backbone.computedfields.js' | ||
}, | ||
amd: { | ||
src: ['<banner:meta.banner>', 'src/amd.js'], | ||
dest: 'lib/amd/backbone.computedfields.js' | ||
} | ||
}, | ||
|
||
min: { | ||
standard: { | ||
src: ['<banner:meta.banner>', '<config:rig.build.dest>'], | ||
dest: 'lib/backbone.computedfields.min.js' | ||
}, | ||
amd: { | ||
src: ['<banner:meta.banner>', '<config:rig.amd.dest>'], | ||
dest: 'lib/amd/backbone.computedfields.min.js' | ||
}, | ||
}, | ||
|
||
jshint: { | ||
options: { | ||
curly: true, | ||
eqeqeq: true, | ||
immed: false, | ||
latedef: true, | ||
newcap: true, | ||
noarg: true, | ||
sub: true, | ||
undef: true, | ||
boss: true, | ||
eqnull: true, | ||
browser: true | ||
}, | ||
globals: { | ||
jQuery: true, | ||
Backbone: true, | ||
_: true, | ||
Marionette: true, | ||
$: true, | ||
slice: true | ||
} | ||
}, | ||
uglify: {} | ||
}); | ||
|
||
// Default task. | ||
grunt.registerTask('default', 'lint rig min'); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Backbone.ComputedFields, v0.0.5 | ||
// Copyright (c)2013 alexander.beletsky@gmail.com | ||
// Distributed under MIT license | ||
// http://github.com/alexander.beletsky/backbone.clickdebounce | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
|
||
var underscore = require('underscore'); | ||
var backbone = require('backbone'); | ||
|
||
module.exports = factory(underscore, backbone); | ||
|
||
} else if (typeof define === 'function' && define.amd) { | ||
|
||
define(['underscore', 'backbone'], factory); | ||
|
||
} | ||
}(this, function (_, Backbone) { | ||
|
||
Backbone.ComputedFields = (function(Backbone, _){ | ||
|
||
var ComputedFields = function (model) { | ||
this.model = model; | ||
this._computedFields = []; | ||
|
||
this.initialize(); | ||
}; | ||
|
||
ComputedFields.VERSION = '0.0.5'; | ||
|
||
_.extend(ComputedFields.prototype, { | ||
initialize: function () { | ||
_.bindAll(this); | ||
|
||
this._lookUpComputedFields(); | ||
this._bindModelEvents(); | ||
this._wrapJSON(); | ||
}, | ||
|
||
_lookUpComputedFields: function () { | ||
for (var obj in this.model.computed) { | ||
var field = this.model.computed[obj]; | ||
|
||
if (field && (field.set || field.get)) { | ||
this._computedFields.push({name: obj, field: field}); | ||
} | ||
} | ||
}, | ||
|
||
_bindModelEvents: function () { | ||
_.each(this._computedFields, function (computedField) { | ||
var fieldName = computedField.name; | ||
var field = computedField.field; | ||
|
||
var updateComputed = _.bind(function () { | ||
var value = this._computeFieldValue(field); | ||
this.model.set(fieldName, value, { skipChangeEvent: true }); | ||
}, this); | ||
|
||
var updateDependent = _.bind(function (model, value, options) { | ||
if (options && options.skipChangeEvent) { | ||
return; | ||
} | ||
|
||
if (field.set) { | ||
var fields = this._dependentFields(field.depends); | ||
value = value || this.model.get(fieldName); | ||
|
||
field.set.call(this.model, value, fields); | ||
this.model.set(fields, options); | ||
} | ||
}, this); | ||
|
||
this._thenDependentChanges(field.depends, updateComputed); | ||
this._thenComputedChanges(fieldName, updateDependent); | ||
|
||
if (this._isModelInitialized()) { | ||
updateComputed(); | ||
} | ||
}, this); | ||
}, | ||
|
||
_isModelInitialized: function () { | ||
return !_.isEmpty(this.model.attributes); | ||
}, | ||
|
||
_thenDependentChanges: function (depends, callback) { | ||
_.each(depends, function (name) { | ||
if (typeof (name) === 'string') { | ||
this.model.on('change:' + name, callback); | ||
} | ||
|
||
if (typeof (name) === 'function') { | ||
name.call(this.model, callback); | ||
} | ||
}, this); | ||
}, | ||
|
||
_thenComputedChanges: function (fieldName, callback) { | ||
this.model.on('change:' + fieldName, callback); | ||
}, | ||
|
||
_wrapJSON: function () { | ||
this.model.toJSON = _.wrap(this.model.toJSON, this._toJSON); | ||
}, | ||
|
||
_toJSON: function (toJSON) { | ||
var attributes = toJSON.call(this.model); | ||
|
||
var stripped = _.reduce(this._computedFields, function (memo, computed) { | ||
if (computed.field.toJSON === false) { | ||
memo.push(computed.name); | ||
} | ||
return memo; | ||
},[]); | ||
|
||
return _.omit(attributes, stripped); | ||
}, | ||
|
||
_computeFieldValue: function (computedField) { | ||
if (computedField && computedField.get) { | ||
var fields = this._dependentFields(computedField.depends); | ||
return computedField.get.call(this.model, fields); | ||
} | ||
}, | ||
|
||
_dependentFields: function (depends) { | ||
return _.reduce(depends, function (memo, field) { | ||
memo[field] = this.model.get(field); | ||
return memo; | ||
}, {}, this); | ||
} | ||
|
||
}); | ||
|
||
return ComputedFields; | ||
|
||
})(Backbone, _); | ||
return Backbone.ComputedFields; | ||
|
||
})); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Backbone.ComputedFields, v0.0.5 | ||
// Copyright (c)2013 alexander.beletsky@gmail.com | ||
// Distributed under MIT license | ||
// http://github.com/alexander.beletsky/backbone.clickdebounce | ||
Backbone.ComputedFields = (function(Backbone, _){ | ||
|
||
var ComputedFields = function (model) { | ||
this.model = model; | ||
this._computedFields = []; | ||
|
||
this.initialize(); | ||
}; | ||
|
||
ComputedFields.VERSION = '0.0.5'; | ||
|
||
_.extend(ComputedFields.prototype, { | ||
initialize: function () { | ||
_.bindAll(this); | ||
|
||
this._lookUpComputedFields(); | ||
this._bindModelEvents(); | ||
this._wrapJSON(); | ||
}, | ||
|
||
_lookUpComputedFields: function () { | ||
for (var obj in this.model.computed) { | ||
var field = this.model.computed[obj]; | ||
|
||
if (field && (field.set || field.get)) { | ||
this._computedFields.push({name: obj, field: field}); | ||
} | ||
} | ||
}, | ||
|
||
_bindModelEvents: function () { | ||
_.each(this._computedFields, function (computedField) { | ||
var fieldName = computedField.name; | ||
var field = computedField.field; | ||
|
||
var updateComputed = _.bind(function () { | ||
var value = this._computeFieldValue(field); | ||
this.model.set(fieldName, value, { skipChangeEvent: true }); | ||
}, this); | ||
|
||
var updateDependent = _.bind(function (model, value, options) { | ||
if (options && options.skipChangeEvent) { | ||
return; | ||
} | ||
|
||
if (field.set) { | ||
var fields = this._dependentFields(field.depends); | ||
value = value || this.model.get(fieldName); | ||
|
||
field.set.call(this.model, value, fields); | ||
this.model.set(fields, options); | ||
} | ||
}, this); | ||
|
||
this._thenDependentChanges(field.depends, updateComputed); | ||
this._thenComputedChanges(fieldName, updateDependent); | ||
|
||
if (this._isModelInitialized()) { | ||
updateComputed(); | ||
} | ||
}, this); | ||
}, | ||
|
||
_isModelInitialized: function () { | ||
return !_.isEmpty(this.model.attributes); | ||
}, | ||
|
||
_thenDependentChanges: function (depends, callback) { | ||
_.each(depends, function (name) { | ||
if (typeof (name) === 'string') { | ||
this.model.on('change:' + name, callback); | ||
} | ||
|
||
if (typeof (name) === 'function') { | ||
name.call(this.model, callback); | ||
} | ||
}, this); | ||
}, | ||
|
||
_thenComputedChanges: function (fieldName, callback) { | ||
this.model.on('change:' + fieldName, callback); | ||
}, | ||
|
||
_wrapJSON: function () { | ||
this.model.toJSON = _.wrap(this.model.toJSON, this._toJSON); | ||
}, | ||
|
||
_toJSON: function (toJSON) { | ||
var attributes = toJSON.call(this.model); | ||
|
||
var stripped = _.reduce(this._computedFields, function (memo, computed) { | ||
if (computed.field.toJSON === false) { | ||
memo.push(computed.name); | ||
} | ||
return memo; | ||
},[]); | ||
|
||
return _.omit(attributes, stripped); | ||
}, | ||
|
||
_computeFieldValue: function (computedField) { | ||
if (computedField && computedField.get) { | ||
var fields = this._dependentFields(computedField.depends); | ||
return computedField.get.call(this.model, fields); | ||
} | ||
}, | ||
|
||
_dependentFields: function (depends) { | ||
return _.reduce(depends, function (memo, field) { | ||
memo[field] = this.model.get(field); | ||
return memo; | ||
}, {}, this); | ||
} | ||
|
||
}); | ||
|
||
return ComputedFields; | ||
|
||
})(Backbone, _); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.