Comparing version 0.0.3 to 0.0.4
@@ -6,3 +6,3 @@ /** | ||
var Sync = require('../src/Sync'), | ||
var Sync = require('../src/Sync').Sync, | ||
Is = require('neuro-is').Is; | ||
@@ -140,2 +140,8 @@ | ||
/** | ||
* Process the response data before it's added or set by the Collection or Model during request response. | ||
* This should be refactored by your super Class of Collection or Model. | ||
* @param {Object} response The response object from the request | ||
* @return {Object} The returned object would be used to add / set in the Collection or Model. Nothing returned means nothing is added / set. | ||
*/ | ||
process: function(response){ | ||
@@ -142,0 +148,0 @@ return response; |
@@ -15,5 +15,5 @@ (function(modules) { | ||
var Neuro = require("1"); | ||
Neuro.Sync = require("7"); | ||
Neuro.Model = require("8"); | ||
Neuro.Collection = require("a"); | ||
Neuro.Sync = require("b").Sync; | ||
Neuro.Model = require("c").Model; | ||
Neuro.Collection = require("e").Collection; | ||
exports = module.exports = Neuro; | ||
@@ -23,4 +23,5 @@ }, | ||
var Neuro = require("2"); | ||
Neuro.Model = require("3"); | ||
Neuro.Collection = require("6"); | ||
Neuro.Model = require("3").Model; | ||
Neuro.Collection = require("9").Collection; | ||
Neuro.View = require("a").View; | ||
exports = module.exports = Neuro; | ||
@@ -30,3 +31,3 @@ }, | ||
var Neuro = { | ||
version: "0.1.7" | ||
version: "0.1.9" | ||
}; | ||
@@ -36,17 +37,39 @@ exports = module.exports = Neuro; | ||
"3": function(require, module, exports, global) { | ||
var Is = require("4").Is, Silence = require("5"); | ||
var createGetter = function(type) { | ||
var Is = require("4").Is, Silence = require("5").Silence, Connector = require("6").Connector, CustomAccessor = require("8").CustomAccessor; | ||
var curryGetter = function(type) { | ||
var isPrevious = type == "_previousData" || void 0; | ||
return function(prop) { | ||
var val = this[type][prop], accessor = this.getAccessor(prop), getter = accessor && accessor.get; | ||
return getter ? getter.call(this, isPrevious) : val; | ||
var accessor = this.getAccessor(prop, isPrevious ? "getPrevious" : "get"); | ||
return accessor ? accessor() : this[type][prop]; | ||
}.overloadGetter(); | ||
}; | ||
var curryGetData = function(type) { | ||
return function() { | ||
var props = this.keys(), obj = {}; | ||
props.each(function(prop) { | ||
var val = this[type](prop); | ||
switch (typeOf(val)) { | ||
case "array": | ||
val = val.slice(); | ||
break; | ||
case "object": | ||
if (!val.$constructor || val.$constructor && !instanceOf(val.$constructor, Class)) { | ||
val = Object.clone(val); | ||
} | ||
break; | ||
} | ||
obj[prop] = val; | ||
}.bind(this)); | ||
return obj; | ||
}; | ||
}; | ||
var Model = new Class({ | ||
Implements: [ Events, Options, Silence ], | ||
Implements: [ Connector, CustomAccessor, Events, Options, Silence ], | ||
primaryKey: undefined, | ||
_data: {}, | ||
_defaults: {}, | ||
_changed: false, | ||
_changedProperties: {}, | ||
_previousData: {}, | ||
_setting: 0, | ||
_accessors: {}, | ||
@@ -56,4 +79,3 @@ options: { | ||
accessors: {}, | ||
defaults: {}, | ||
silent: false | ||
defaults: {} | ||
}, | ||
@@ -69,6 +91,7 @@ initialize: function(data, options) { | ||
this.primaryKey = this.options.primaryKey; | ||
this.__set(this.options.defaults); | ||
this._resetChanged(); | ||
this.setAccessor(this.options.accessors); | ||
this.silence(this.options.silent); | ||
this.setupAccessors(); | ||
Object.merge(this._defaults, this.options.defaults); | ||
this.silence(function() { | ||
this.set(this._defaults); | ||
}.bind(this)); | ||
if (data) { | ||
@@ -80,37 +103,47 @@ this.set(data); | ||
__set: function(prop, val) { | ||
var accessor = this.getAccessor(prop), setter = accessor && accessor.set, setterVal; | ||
if (setter) { | ||
setterVal = setter.apply(this, arguments); | ||
var accessor = this.getAccessor(prop, "set"); | ||
if (accessor) { | ||
return accessor.apply(this, arguments); | ||
} | ||
this._changed = true; | ||
this._data[prop] = this._changedProperties[prop] = setter && setterVal !== null ? setterVal : val; | ||
var old = this.get(prop); | ||
if (!Is.Equal(old, val)) { | ||
switch (typeOf(val)) { | ||
case "array": | ||
val = val.slice(); | ||
break; | ||
case "object": | ||
if (!val.$constructor || val.$constructor && !instanceOf(val.$constructor, Class)) { | ||
val = Object.clone(val); | ||
} | ||
break; | ||
} | ||
this._changed = true; | ||
this._data[prop] = this._changedProperties[prop] = val; | ||
return this; | ||
} | ||
return this; | ||
}.overloadSetter(), | ||
_set: function(prop, val) { | ||
var old = this._data[prop], accessor = this.getAccessor(prop), setter = accessor && accessor.set, setterVal; | ||
switch (typeOf(val)) { | ||
case "array": | ||
val = val.slice(); | ||
break; | ||
case "object": | ||
if (!val.$constructor || val.$constructor && !instanceOf(val.$constructor, Class)) { | ||
val = Object.clone(val); | ||
} | ||
break; | ||
} | ||
if (!Is.Equal(old, val)) { | ||
this.__set(prop, val); | ||
} | ||
this._setting++; | ||
this.__set(prop, val); | ||
this._setting--; | ||
return this; | ||
}.overloadSetter(), | ||
}, | ||
set: function(prop, val) { | ||
var isSetting; | ||
if (prop) { | ||
this._setPreviousData(); | ||
isSetting = this.isSetting(); | ||
!isSetting && this._setPrevious(this.getData()); | ||
this._set(prop, val); | ||
this.changeProperty(this._changedProperties); | ||
this.change(); | ||
this._resetChanged(); | ||
if (!isSetting) { | ||
this.changeProperty(this._changedProperties); | ||
this.change(); | ||
this._resetChanged(); | ||
} | ||
} | ||
return this; | ||
}, | ||
isSetting: function() { | ||
return !!this._setting; | ||
}, | ||
unset: function(prop) { | ||
@@ -133,6 +166,6 @@ var props = {}, len, i = 0, item; | ||
item = prop[i++]; | ||
props[item] = this.options.defaults[item]; | ||
props[item] = this._defaults[item]; | ||
} | ||
} else { | ||
props = this.options.defaults; | ||
props = this._defaults; | ||
} | ||
@@ -143,14 +176,10 @@ this.set(props); | ||
}, | ||
get: createGetter("_data"), | ||
getData: function() { | ||
return Object.clone(this._data); | ||
}, | ||
_setPreviousData: function() { | ||
this._previousData = Object.clone(this._data); | ||
get: curryGetter("_data"), | ||
getData: curryGetData("get"), | ||
_setPrevious: function(prop, val) { | ||
this._previousData[prop] = val; | ||
return this; | ||
}, | ||
getPrevious: createGetter("_previousData"), | ||
getPreviousData: function() { | ||
return Object.clone(this._previousData); | ||
}, | ||
}.overloadSetter(), | ||
getPrevious: curryGetter("_previousData"), | ||
getPreviousData: curryGetData("getPrevious"), | ||
_resetChanged: function() { | ||
@@ -171,3 +200,3 @@ if (this._changed) { | ||
if (this._changed) { | ||
this.signalChangeProperty(prop, val); | ||
this.signalChangeProperty(prop, val, this.getPrevious(prop)); | ||
} | ||
@@ -181,15 +210,15 @@ return this; | ||
signalChange: function() { | ||
!this.isSilent() && this.fireEvent("change"); | ||
!this.isSilent() && this.fireEvent("change", this); | ||
return this; | ||
}, | ||
signalChangeProperty: function(prop, val) { | ||
!this.isSilent() && this.fireEvent("change:" + prop, [ prop, val ]); | ||
signalChangeProperty: function(prop, newVal, oldVal) { | ||
!this.isSilent() && this.fireEvent("change:" + prop, [ this, prop, newVal, oldVal ]); | ||
return this; | ||
}, | ||
signalDestroy: function() { | ||
!this.isSilent() && this.fireEvent("destroy"); | ||
!this.isSilent() && this.fireEvent("destroy", this); | ||
return this; | ||
}, | ||
signalReset: function() { | ||
!this.isSilent() && this.fireEvent("reset"); | ||
!this.isSilent() && this.fireEvent("reset", this); | ||
return this; | ||
@@ -200,14 +229,14 @@ }, | ||
}, | ||
setAccessor: function(key, val) { | ||
this._accessors[key] = val; | ||
spy: function(prop, callback) { | ||
if (typeOf(prop) == "string" && prop in this._data && typeOf(callback) == "function") { | ||
this.addEvent("change:" + prop, callback); | ||
} | ||
return this; | ||
}.overloadSetter(), | ||
getAccessor: function(key) { | ||
return this._accessors[key]; | ||
}.overloadGetter(), | ||
unsetAccessor: function(key) { | ||
delete this._accessors[key]; | ||
this._accessors[key] = undefined; | ||
unspy: function(prop, callback) { | ||
if (typeOf(prop) == "string" && prop in this._data) { | ||
this.removeEvents("change:" + prop, callback); | ||
} | ||
return this; | ||
} | ||
}.overloadSetter() | ||
}); | ||
@@ -219,3 +248,3 @@ [ "subset", "map", "filter", "every", "some", "keys", "values", "getLength", "keyOf", "contains", "toQueryString" ].each(function(method) { | ||
}); | ||
module.exports = Model; | ||
exports.Model = Model; | ||
}, | ||
@@ -334,5 +363,7 @@ "4": function(require, module, exports, global) { | ||
var Silence = new Class({ | ||
_silent: false, | ||
silence: function(silent) { | ||
this._silent = !!silent; | ||
_silent: 0, | ||
silence: function(fnc) { | ||
this._silent++; | ||
fnc(); | ||
this._silent--; | ||
return this; | ||
@@ -344,11 +375,127 @@ }, | ||
}); | ||
exports = module.exports = Silence; | ||
exports.Silence = Silence; | ||
}, | ||
"6": function(require, module, exports, global) { | ||
var Model = require("3"), Silence = require("5"); | ||
require("7"); | ||
var processFn = function(type, evt, fn, obj) { | ||
if (type == "string") { | ||
fn = obj[fn] ? obj.bound(fn) : undefined; | ||
} | ||
return fn; | ||
}; | ||
var mapSubEvents = function(obj, baseEvt) { | ||
var map = {}; | ||
Object.each(obj, function(val, key) { | ||
if (key == "*") { | ||
key = baseEvt; | ||
} else { | ||
key = baseEvt + ":" + key; | ||
} | ||
map[key] = val; | ||
}); | ||
return map; | ||
}; | ||
var process = function(methodStr, map, obj) { | ||
Object.each(map, function(methods, evt) { | ||
methods = Array.from(methods); | ||
methods.each(function(method) { | ||
var type = typeOf(method); | ||
switch (type) { | ||
case "object": | ||
if (!instanceOf(method, Class)) { | ||
process.call(this, methodStr, mapSubEvents(method, evt), obj); | ||
} | ||
break; | ||
case "string": | ||
case "function": | ||
method = processFn.call(this, type, evt, method, obj); | ||
method && this[methodStr](evt, method); | ||
break; | ||
} | ||
}, this); | ||
}, this); | ||
}; | ||
var curryConnection = function(str) { | ||
var methodStr = str == "connect" ? "addEvent" : "removeEvent"; | ||
return function(obj, oneWay) { | ||
if (obj && typeOf(obj[str]) == "function") { | ||
var map = this.options.connector; | ||
process.call(this, methodStr, map, obj); | ||
!oneWay && obj[str](this, true); | ||
} | ||
return this; | ||
}; | ||
}; | ||
var Connector = new Class({ | ||
Implements: [ Class.Binds ], | ||
connect: curryConnection("connect"), | ||
disconnect: curryConnection("disconnect") | ||
}); | ||
exports.Connector = Connector; | ||
}, | ||
"7": function(require, module, exports, global) { | ||
Class.Binds = new Class({ | ||
$bound: {}, | ||
bound: function(name) { | ||
return this.$bound[name] ? this.$bound[name] : this.$bound[name] = this[name].bind(this); | ||
} | ||
}); | ||
}, | ||
"8": function(require, module, exports, global) { | ||
var accessTypes = [ "set", "get", "getPrevious" ], getMap = { | ||
get: false, | ||
getPrevious: true | ||
}; | ||
var CustomAccessor = new Class({ | ||
_accessors: {}, | ||
options: { | ||
accessors: {} | ||
}, | ||
setupAccessors: function() { | ||
this.setAccessor(Object.merge({}, this._accessors, this.options.accessors)); | ||
return this; | ||
}, | ||
setAccessor: function(name, val) { | ||
var accessors = {}, cont = Object.keys(val).some(accessTypes.contains, accessTypes); | ||
if (cont) { | ||
if (val.get && !val.getPrevious) { | ||
accessors.getPrevious = val.get.bind(this, true); | ||
accessors.get = val.get.bind(this, false); | ||
} | ||
val.set && (accessors.set = val.set.bind(this)); | ||
Object.each(getMap, function(bool, type) { | ||
if (val[type] && !accessors[type]) { | ||
accessors[type] = val[type].bind(this, bool); | ||
} | ||
}, this); | ||
this._accessors[name] = accessors; | ||
} | ||
return this; | ||
}.overloadSetter(), | ||
getAccessor: function(name, type) { | ||
var accessors = this._accessors[name]; | ||
if (type && accessors && accessors[type]) { | ||
accessors = accessors[type]; | ||
} | ||
return accessors; | ||
}, | ||
unsetAccessor: function(name, type) { | ||
if (name && type) { | ||
delete this._accessors[name][type]; | ||
} else { | ||
delete this._accessors[name]; | ||
this._accessors[name] = undefined; | ||
} | ||
return this; | ||
} | ||
}); | ||
exports.CustomAccessor = CustomAccessor; | ||
}, | ||
"9": function(require, module, exports, global) { | ||
var Model = require("3").Model, Silence = require("5").Silence, Connector = require("6").Connector; | ||
var Collection = new Class({ | ||
Implements: [ Events, Options, Silence ], | ||
Implements: [ Connector, Events, Options, Silence ], | ||
_models: [], | ||
_bound: {}, | ||
_Model: Model, | ||
length: 0, | ||
primaryKey: undefined, | ||
@@ -358,4 +505,3 @@ options: { | ||
Model: undefined, | ||
modelOptions: undefined, | ||
silent: false | ||
modelOptions: undefined | ||
}, | ||
@@ -367,5 +513,2 @@ initialize: function(models, options) { | ||
this.setOptions(options); | ||
this._bound = { | ||
remove: this.remove.bind(this) | ||
}; | ||
this.primaryKey = this.options.primaryKey; | ||
@@ -375,3 +518,2 @@ if (this.options.Model) { | ||
} | ||
this.silence(this.options.silent); | ||
if (models) { | ||
@@ -393,7 +535,12 @@ this.add(models); | ||
}, | ||
_add: function(model) { | ||
_add: function(model, at) { | ||
model = new this._Model(model, this.options.modelOptions); | ||
if (!this.hasModel(model)) { | ||
model.addEvent("destroy", this._bound.remove); | ||
this._models.push(model); | ||
model.addEvent("destroy", this.bound("remove")); | ||
if (at != undefined) { | ||
this._models.splice(at, 0, model); | ||
} else { | ||
this._models.push(model); | ||
} | ||
this.length = this._models.length; | ||
this.signalAdd(model); | ||
@@ -403,3 +550,3 @@ } | ||
}, | ||
add: function(models) { | ||
add: function(models, at) { | ||
models = Array.from(models); | ||
@@ -424,4 +571,5 @@ var len = models.length, i = 0; | ||
_remove: function(model) { | ||
model.removeEvent("destroy", this._bound.remove); | ||
model.removeEvent("destroy", this.bound("remove")); | ||
this._models.erase(model); | ||
this.length = this._models.length; | ||
this.signalRemove(model); | ||
@@ -453,2 +601,12 @@ return this; | ||
}, | ||
sort: function(fnc) { | ||
this._models.sort(fnc); | ||
this.signalSort(); | ||
return this; | ||
}, | ||
reverse: function() { | ||
this._models.reverse(); | ||
this.signalSort(); | ||
return this; | ||
}, | ||
empty: function() { | ||
@@ -460,13 +618,17 @@ this.remove(this._models); | ||
signalAdd: function(model) { | ||
!this.isSilent() && this.fireEvent("add", model); | ||
!this.isSilent() && this.fireEvent("add", [ this, model ]); | ||
return this; | ||
}, | ||
signalRemove: function(model) { | ||
!this.isSilent() && this.fireEvent("remove", model); | ||
!this.isSilent() && this.fireEvent("remove", [ this, model ]); | ||
return this; | ||
}, | ||
signalEmpty: function() { | ||
!this.isSilent() && this.fireEvent("empty"); | ||
!this.isSilent() && this.fireEvent("empty", this); | ||
return this; | ||
}, | ||
signalSort: function() { | ||
!this.isSilent() && this.fireEvent("sort", this); | ||
return this; | ||
}, | ||
toJSON: function() { | ||
@@ -483,5 +645,92 @@ return this.map(function(model) { | ||
}); | ||
module.exports = Collection; | ||
exports.Collection = Collection; | ||
}, | ||
"7": function(require, module, exports, global) { | ||
a: function(require, module, exports, global) { | ||
var Connector = require("6").Connector; | ||
var eventHandler = function(handler) { | ||
return function() { | ||
var events = this.options.events, element = this.element; | ||
if (element && events) { | ||
Object.each(events, function(val, key) { | ||
var methods = Array.from(val), len = methods.length, i = 0, method; | ||
while (len--) { | ||
method = methods[i++]; | ||
this.element[handler](key, typeOf(method) == "function" ? method : this.bound(method)); | ||
} | ||
}, this); | ||
} | ||
return this; | ||
}; | ||
}; | ||
var View = new Class({ | ||
Implements: [ Connector, Events, Options ], | ||
options: { | ||
dataObjects: [], | ||
events: {} | ||
}, | ||
initialize: function(element, options) { | ||
this.setup(element, options); | ||
}, | ||
setup: function(element, options) { | ||
element = this.element = document.id(element); | ||
this.setOptions(options); | ||
if (element) { | ||
this.attachEvents(); | ||
this.connectObjects(); | ||
} | ||
return this; | ||
}, | ||
attachEvents: eventHandler("addEvent"), | ||
detachEvents: eventHandler("removeEvent"), | ||
connectObjects: function() { | ||
Array.from(this.options.dataObjects).each(this.connect.bind(this)); | ||
return this; | ||
}, | ||
disconnectObjects: function() { | ||
Array.from(this.options.dataObjects).each(this.disconnect.bind(this)); | ||
return this; | ||
}, | ||
create: function() { | ||
return this; | ||
}, | ||
render: function() { | ||
this.signalRender(); | ||
return this; | ||
}, | ||
inject: function(reference, where) { | ||
this.element.inject(reference, where); | ||
this.signalInject(); | ||
return this; | ||
}, | ||
dispose: function() { | ||
this.element.dispose(); | ||
this.signalDispose(); | ||
return this; | ||
}, | ||
destroy: function() { | ||
var element = this.element; | ||
element && (this.detachEvents(), this.disconnectObjects(), element.destroy(), this.element = undefined); | ||
this.signalDestroy(); | ||
return this; | ||
}, | ||
signalRender: function() { | ||
this.fireEvent("render"); | ||
return this; | ||
}, | ||
signalInject: function() { | ||
this.fireEvent("inject"); | ||
return this; | ||
}, | ||
signalDispose: function() { | ||
this.fireEvent("dispose"); | ||
return this; | ||
}, | ||
signalDestroy: function() { | ||
this.fireEvent("destroy"); | ||
return this; | ||
} | ||
}); | ||
exports.View = View; | ||
}, | ||
b: function(require, module, exports, global) { | ||
var REST = function(type) { | ||
@@ -502,8 +751,8 @@ return function() { | ||
}); | ||
module.exports = Sync; | ||
exports.Sync = Sync; | ||
}, | ||
"8": function(require, module, exports, global) { | ||
var Neuro = require("1"), Sync = require("7"), Mixins = require("9"); | ||
c: function(require, module, exports, global) { | ||
var modelObj = require("3"), Sync = require("b").Sync, Mixins = require("d"); | ||
var Model = new Class({ | ||
Extends: Neuro.Model, | ||
Extends: modelObj.Model, | ||
Implements: [ Mixins.Sync ], | ||
@@ -537,8 +786,4 @@ _new: true, | ||
}, | ||
save: function(prop, val, callback) { | ||
var isNew = this.isNew(), method = [ "create", "update" ][+isNew], data; | ||
if (prop) { | ||
this.set(prop, val); | ||
} | ||
data = this.toJSON(); | ||
save: function(callback) { | ||
var isNew = this.isNew(), method = [ "create", "update" ][+isNew], data = this.toJSON(); | ||
this.sync(method, data, function(response) { | ||
@@ -580,6 +825,6 @@ this._syncSave(response, callback); | ||
}); | ||
module.exports = Model; | ||
modelObj.Model = exports.Model = Model; | ||
}, | ||
"9": function(require, module, exports, global) { | ||
var Sync = require("7"), Is = require("4").Is; | ||
d: function(require, module, exports, global) { | ||
var Sync = require("b").Sync, Is = require("4").Is; | ||
var SyncSignals = {}, signalSyncPrefix = "signalSync", syncPrefix = "sync:", syncFnc = function(str) { | ||
@@ -685,10 +930,10 @@ return function() { | ||
}, | ||
a: function(require, module, exports, global) { | ||
var Neuro = require("1"), Sync = require("7"), Mixins = require("9"); | ||
e: function(require, module, exports, global) { | ||
var collectionObj = require("9"), Model = require("c").Model, Sync = require("b").Sync, Mixins = require("d"); | ||
var Collection = new Class({ | ||
Extends: Neuro.Collection, | ||
Extends: collectionObj.Collection, | ||
Implements: [ Mixins.Sync ], | ||
options: { | ||
request: {}, | ||
Model: Neuro.Model | ||
Model: Model | ||
}, | ||
@@ -717,4 +962,4 @@ setup: function(models, options) { | ||
}); | ||
module.exports = Collection; | ||
collectionObj.Collection = exports.Collection = Collection; | ||
} | ||
}); |
{ | ||
"name": "neuro-sync", | ||
"description": "An extension of Neuro to provide a \"sync\" component.", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"license": "MIT (http://mootools.net/license.txt)", | ||
@@ -6,0 +6,0 @@ "main": "src/main.js", |
@@ -13,8 +13,9 @@ /** | ||
var Neuro = require('Neuro'), | ||
Sync = require('./Sync'), | ||
var collectionObj = require('Neuro/src/Collection'), | ||
Model = require('./Model').Model, | ||
Sync = require('./Sync').Sync, | ||
Mixins = require('../mixins/sync'); | ||
var Collection = new Class({ | ||
Extends: Neuro.Collection, | ||
Extends: collectionObj.Collection, | ||
@@ -25,3 +26,3 @@ Implements: [Mixins.Sync], | ||
request: {}, | ||
Model: Neuro.Model | ||
Model: Model | ||
}, | ||
@@ -63,2 +64,2 @@ | ||
module.exports = Collection; | ||
collectionObj.Collection = exports.Collection = Collection; |
var Neuro = require('Neuro'); | ||
Neuro.Sync = require('./Sync'); | ||
Neuro.Model = require('./Model'); | ||
Neuro.Collection = require('./Collection'); | ||
Neuro.Sync = require('./Sync').Sync; | ||
Neuro.Model = require('./Model').Model; | ||
Neuro.Collection = require('./Collection').Collection; | ||
exports = module.exports = Neuro; |
@@ -13,8 +13,8 @@ /** | ||
var Neuro = require('Neuro'), | ||
Sync = require('./Sync'), | ||
var modelObj = require('Neuro/src/Model'), | ||
Sync = require('./Sync').Sync, | ||
Mixins = require('../mixins/sync'); | ||
var Model = new Class({ | ||
Extends: Neuro.Model, | ||
Extends: modelObj.Model, | ||
@@ -66,15 +66,8 @@ Implements: [Mixins.Sync], | ||
save: function(prop, val, callback){ | ||
save: function(callback){ | ||
// Determine whether method is create or update; | ||
var isNew = this.isNew(), | ||
method = ['create', 'update'][+isNew], | ||
data; | ||
data = this.toJSON(); | ||
// Set data if property exists | ||
if (prop) { | ||
this.set(prop, val); | ||
} | ||
data = this.toJSON(); | ||
// Issue create/update command to server | ||
@@ -137,2 +130,2 @@ this.sync(method, data, function(response){ | ||
module.exports = Model; | ||
modelObj.Model =exports.Model = Model; |
@@ -30,2 +30,2 @@ /** | ||
module.exports = Sync; | ||
exports.Sync = Sync; |
@@ -257,3 +257,3 @@ buster.testCase('Neuro Sync Model', { | ||
model.save(void 0, void 0, function(response){ | ||
model.save(function(response){ | ||
spy(response.count); | ||
@@ -265,3 +265,3 @@ assert.calledWith(spy, 1); | ||
}); | ||
}).save(void 0, void 0, function(response){ | ||
}).save(function(response){ | ||
spy(response.count); | ||
@@ -300,19 +300,2 @@ assert.calledWith(spy, 2); | ||
'save should set prop/values before sending a request if prop/values are passed to the method': function(){ | ||
var spy = this.spy(), | ||
model = this.mockSyncModel, | ||
response = this.mockResponse, | ||
server = this.server; | ||
model.addEvent('save', function(response){spy();}); | ||
model.save({firstName: 'Patrick'}); | ||
assert.equals(model.get('firstName'), 'Patrick'); | ||
server.respond([response.code, response.contentType, '{}']); | ||
assert.called(spy); | ||
}, | ||
'save should send the model\'s data': function(){ | ||
@@ -319,0 +302,0 @@ var spy = this.spy(), |
219311
6417