Created
July 5, 2011 09:03
-
-
Save limptwiglet/1064518 to your computer and use it in GitHub Desktop.
Create brand method
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
// Client Model | |
CoreIo.Client = SC.Record.extend( | |
/** @scope CoreIo.Client.prototype */ { | |
primaryKey: 'id', | |
title: SC.Record.attr(String), | |
stat: SC.Record.attr(Number, {key: 'status'}), | |
created: SC.Record.attr(SC.DateTime, {format: '%Y-%m-%d %H:%M:%S'}), | |
updated: SC.Record.attr(SC.DateTime, {format: '%Y-%m-%d %H:%M:%S'}), | |
brands: SC.Record.toMany('CoreIo.Brand', {isMaster: YES, inverse: 'client'}), | |
treeItemChildren: function () { | |
return this.get('brands'); | |
}.property() | |
}); | |
// Brand Model | |
CoreIo.Brand = SC.Record.extend( | |
/** @scope CoreIo.Brand.prototype */ { | |
primaryKey: 'id', | |
title: SC.Record.attr(String), | |
stat: SC.Record.attr(Number, {key: 'status'}), | |
created: SC.Record.attr(SC.DateTime, {format: '%Y-%m-%d %H:%M:%S'}), | |
updated: SC.Record.attr(SC.DateTime, {format: '%Y-%m-%d %H:%M:%S'}), | |
client: SC.Record.toOne('CoreIo.Client', {isMaster: NO, inverse: 'brands', key: 'client_id'}), | |
projects: SC.Record.toMany('CoreIo.Project', {isMaster: YES, inverse: 'brand_id'}), | |
treeItemChildren: function () { | |
return null; | |
}.property('id').cacheable() | |
}); | |
CoreIo.createBrand = function (brand, client) { | |
var data = brand; | |
data.client_id = client.get('id'); | |
SC.Request.postUrl('/api/brand') | |
.json() | |
.notify(this, this.brandDidCreate, client) | |
.send(data); | |
}; | |
CoreIo.brandDidCreate = function (request, client) { | |
console.log('client:', client.get('id'), client.get('title')); | |
var json = request.get('body'), | |
id = json.id; | |
console.log(json); | |
var ret = CoreIo.store.pushRetrieve(DBs.Brand, id, json); | |
var brand = CoreIo.store.find(DBs.Brand, id); | |
console.log('brand:', brand.get('id'), brand.get('title')); | |
console.log(CoreIo.store.readDataHash(ret)); | |
if (ret) { | |
client.get('brands').pushObject(brand); | |
} | |
}; | |
// Call it like this | |
CoreIo.createBrand({ | |
title: this.get('title') | |
}, this.get('client')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment