Created
January 31, 2014 10:01
-
-
Save kreshikhin/8729359 to your computer and use it in GitHub Desktop.
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
;function Model(){ | |
this.selected = ko.observable(false); | |
this.excluded = ko.observable(false); | |
this.changed = ko.observable(false); | |
this.detectChanges = ko.observable(true); | |
this.deactivated = ko.observable(false); | |
}; | |
Model.prototype.select = function(){ this.selected(true); }; | |
Model.prototype.unselect = function(){ this.selected(false); }; | |
Model.prototype.exclude = function(){ this.excluded(true); }; | |
Model.prototype.include = function(){ this.excluded(false); }; | |
Model.prototype.deactivate = function(){ this.deactivated(true); }; | |
Model.prototype.activate = function(){ this.deactivated(false); }; | |
Model.prototype.throwChangesTo = function(model){ | |
this.changed.subscribe(function(){ | |
if(model) model.changed(true); | |
}); | |
}; | |
Model.prototype.detectChangesIn = function(followedProperties){ | |
var that = this; | |
for(index in followedProperties){ | |
var property = followedProperties[index]; | |
this[property].subscribe(function(){ | |
if(that.detectChanges()) { | |
that.changed(true); | |
}; | |
}); | |
}; | |
}; | |
Model.prototype.getByIds = function(ids, resource, callback){ | |
var that = this; | |
for(var index in ids){ | |
var id = ids[index]; | |
$.get_json(resource + id).done(function(data){ | |
that.insensibly(function(){ | |
callback(data); | |
}); | |
}); | |
}; | |
}; | |
Model.prototype.getIds = function(collection){ | |
var ids = []; | |
if(collection.constructor == "".constructor) | |
collection = this[collection](); | |
for(var index in collection){ | |
var item = collection[index]; | |
if(!item.excluded()) { | |
ids.push(item.id()); | |
} | |
}; | |
return ids; | |
}; | |
Model.prototype.getValues = function(collection, field){ | |
var values = []; | |
if(collection.constructor == "".constructor) | |
collection = this[collection](); | |
for(var index in collection){ | |
var item = collection[index]; | |
if(!item.excluded()) { | |
values.push(item[field]()); | |
} | |
}; | |
return values; | |
}; | |
Model.prototype.insensibly = function(callback){ | |
var that = this; | |
that.detectChanges(false); | |
that.changed(false); | |
that.excluded(false); | |
callback(); | |
that.detectChanges(true); | |
}; | |
Model.prototype.setResources = function(root, ctors) | |
{ | |
var that = this; | |
if(!ctors) { ctors = {}; }; | |
this.save = function(){ | |
// saves all subresources | |
for(var resource in ctors){ | |
$.each(that[resource](), function(index, item){ | |
if(item.excluded()) return true; | |
$.put_json(resource + '/' + item.id(), item.getData()).done(function(data){ | |
item.setData(data); | |
}); | |
}); | |
}; | |
// saves root resource | |
$.put_json(root + '/' + that.id(), that.getData()).done(function(data){ | |
console.log(that.id(), data); | |
that.setData(data); | |
// reload subresources | |
console.log('save:load'); | |
that.load_subresources(data); | |
that.changed(false); | |
}); | |
}; | |
this.restore = function(callback){ | |
$.get_json('/admin/' + root + '/' + that.id()).done(function(data){ | |
// restore root resource | |
that.setData(data); | |
// restore subresources | |
that.load_subresources(data, callback); | |
that.changed(false); | |
}); | |
}; | |
this.load_subresources = function(data, callback){ | |
$.each(ctors, function(resource, ctor){ | |
var name = ctor.getName(); | |
that[resource]([]); | |
that.getByIds(data[name + '_ids'], resource + '/', function(data){ | |
that[resource].push(new ctor(data, that)); | |
}); | |
}); | |
} | |
// generates add-method for all subresources | |
$.each(ctors, function(resource, ctor){ | |
that['add_' + ctor.getName()] = function(){ | |
$.post_json(resource).done(function(data){ | |
that[resource].unshift(new ctor(data, that)); | |
}); | |
}; | |
}); | |
}; | |
Model.prototype.setEmbedded = function(ctors) | |
{ | |
var that = this; | |
if(!ctors) { ctors = {}; }; | |
$.each(ctors, function(resource, ctor){ | |
that['add_' + ctor.getName()] = function(){ | |
that[resource].unshift(new ctor({}, that)); | |
}; | |
}); | |
}; | |
Model.prototype.setCollections = function(ctors) | |
{ | |
var that = this; | |
$.each(ctors, function(resource, ctor){ | |
that['add_' + ctor.getName()] = function(){ | |
that[resource].unshift(new ctor("", that)); | |
}; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment