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
64 changes: 49 additions & 15 deletions distribution/deep-model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*jshint expr:true eqnull:true */
/**
*
* Backbone.DeepModel v0.10.4
* Backbone.DeepModel v0.11.0
*
* Copyright (c) 2013 Charles Davison, Pow Media Ltd
*
Expand All @@ -14,7 +14,18 @@
*
* Based on https://gist.github.com/echong/3861963
*/
(function() {
;(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'backbone'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('underscore'), require('backbone'));
} else {
// globals
factory(_, Backbone);
}
}(function(_, Backbone) {
var arrays, basicObjects, deepClone, deepExtend, deepExtendCouple, isBasicObject,
__slice = [].slice;

Expand All @@ -26,6 +37,9 @@
if (obj instanceof Backbone.Collection || obj instanceof Backbone.Model) {
return obj;
}
if (_.isElement(obj)) {
return obj;
}
if (_.isDate(obj)) {
return new Date(obj.getTime());
}
Expand Down Expand Up @@ -117,7 +131,7 @@
deepExtend: deepExtend
});

}).call(this);
}));

/**
* Main source
Expand All @@ -127,12 +141,15 @@
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'backbone'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('underscore'), require('backbone'));
} else {
// globals
factory(_, Backbone);
}
}(function(_, Backbone) {

/**
* Takes a nested object and returns a shallow object keyed with the path names
* e.g. { "level1.level2": "value" }
Expand All @@ -147,7 +164,7 @@
for (var key in obj) {
var val = obj[key];

if (val && val.constructor === Object && !_.isEmpty(val)) {
if (val && (val.constructor === Object || val.constructor === Array) && !_.isEmpty(val)) {
//Recursion for embedded objects
var obj2 = objToPaths(val);

Expand All @@ -172,7 +189,7 @@
function getNested(obj, path, return_exists) {
var separator = DeepModel.keyPathSeparator;

var fields = path.split(separator);
var fields = path ? path.split(separator) : [];
var result = obj;
return_exists || (return_exists === false);
for (var i = 0, n = fields.length; i < n; i++) {
Expand All @@ -184,7 +201,7 @@
if (result == null && i < n - 1) {
result = {};
}

if (typeof result === 'undefined') {
if (return_exists)
{
Expand Down Expand Up @@ -212,7 +229,7 @@

var separator = DeepModel.keyPathSeparator;

var fields = path.split(separator);
var fields = path ? path.split(separator) : [];
var result = obj;
for (var i = 0, n = fields.length; i < n && result !== undefined ; i++) {
var field = fields[i];
Expand All @@ -223,7 +240,10 @@
} else {
//Create the child object if it doesn't exist, or isn't an object
if (typeof result[field] === 'undefined' || ! _.isObject(result[field])) {
result[field] = {};
var nextField = fields[i+1];

// create array if next field is integer, else create object
result[field] = /^\d+$/.test(nextField) ? [] : {};
}

//Move onto the next part of the path
Expand Down Expand Up @@ -274,7 +294,7 @@
set: function(key, val, options) {
var attr, attrs, unset, changes, silent, changing, prev, current;
if (key == null) return this;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (typeof key === 'object') {
attrs = key;
Expand All @@ -284,7 +304,7 @@
}

options || (options = {});

// Run validation.
if (!this._validate(attrs, options)) return false;

Expand Down Expand Up @@ -329,11 +349,15 @@

//<custom code>
var separator = DeepModel.keyPathSeparator;
var alreadyTriggered = {}; // * @restorer

for (var i = 0, l = changes.length; i < l; i++) {
var key = changes[i];

this.trigger('change:' + key, this, getNested(current, key), options);
if (!alreadyTriggered.hasOwnProperty(key) || !alreadyTriggered[key]) { // * @restorer
alreadyTriggered[key] = true; // * @restorer
this.trigger('change:' + key, this, getNested(current, key), options);
} // * @restorer

var fields = key.split(separator);

Expand All @@ -342,7 +366,17 @@
var parentKey = _.first(fields, n).join(separator),
wildcardKey = parentKey + separator + '*';

this.trigger('change:' + wildcardKey, this, getNested(current, parentKey), options);
if (!alreadyTriggered.hasOwnProperty(wildcardKey) || !alreadyTriggered[wildcardKey]) { // * @restorer
alreadyTriggered[wildcardKey] = true; // * @restorer
this.trigger('change:' + wildcardKey, this, getNested(current, parentKey), options);
} // * @restorer

// + @restorer
if (!alreadyTriggered.hasOwnProperty(parentKey) || !alreadyTriggered[parentKey]) {
alreadyTriggered[parentKey] = true;
this.trigger('change:' + parentKey, this, getNested(current, parentKey), options);
}
// - @restorer
}
//</custom code>
}
Expand Down Expand Up @@ -388,7 +422,7 @@
//</custom code>

var old = this._changing ? this._previousAttributes : this.attributes;

//<custom code>
diff = objToPaths(diff);
old = objToPaths(old);
Expand Down Expand Up @@ -431,7 +465,7 @@

//For use in NodeJS
if (typeof module != 'undefined') module.exports = DeepModel;

return Backbone;

}));
Expand Down
4 changes: 2 additions & 2 deletions distribution/deep-model.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./src/deep-model');
module.exports = require('./distribution/deep-model');
15 changes: 13 additions & 2 deletions lib/underscore.mixin.deepExtend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
*
* Based on https://gist.github.com/echong/3861963
*/
(function(_, Backbone) {
;(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'backbone'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('underscore'), require('backbone'));
} else {
// globals
factory(_, Backbone);
}
}(function(_, Backbone) {
var arrays, basicObjects, deepClone, deepExtend, deepExtendCouple, isBasicObject,
__slice = [].slice;

Expand Down Expand Up @@ -109,4 +120,4 @@
deepExtend: deepExtend
});

}).call(this, _, Backbone);
}));