Created
March 2, 2012 23:36
-
-
Save krotscheck/1962400 to your computer and use it in GitHub Desktop.
A quick Sproutcore nested document (for use with CouchDB) example.
This file contains hidden or 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 assumes the following document structure: | |
{ | |
email: "[email protected]", | |
firstName: "MyFirstName", | |
lastName: "MyLastName", | |
address: { | |
addressLine1: "Address Line 1", | |
addressLine2: "Address Line 2", | |
city: "Seattle", | |
state: "WA", | |
zip: "98125", | |
} | |
} | |
First there's the address object (needs to be first, look at sc_require on how to sequence these): | |
MyApp.Address = SC.Record.extend( | |
/** @scope MyApp.Address.prototype */ | |
{ | |
/** | |
* The first address line. | |
*/ | |
addressLine1 : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The second address line. | |
*/ | |
addressLine2 : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The city. | |
*/ | |
city : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The state. | |
*/ | |
state : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The zip. | |
*/ | |
zip : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
}); | |
Then there's the user object which makes use of the above: | |
MyApp.User = SC.Record.extend( | |
/** @scope MyApp.User.prototype */ | |
{ | |
/** | |
* User email. | |
*/ | |
email : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The first name of the user | |
*/ | |
firstName : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* Last name of the user. | |
*/ | |
lastName : SC.Record.attr(String, { | |
isRequired : YES | |
}), | |
/** | |
* The fullname of the user. | |
*/ | |
fullName : function() { | |
var firstName = this.get('firstName'); | |
var lastName = this.get('lastName'); | |
if (SC.empty(firstName)) { | |
firstName = ''; | |
} | |
if (SC.empty(lastName)) { | |
lastName = ''; | |
} | |
return "_name_format".loc(firstName, lastName); | |
}.property('lastName', 'firstName').cacheable(), | |
/** | |
* The address of the user. | |
*/ | |
address : SC.Record.toOne('MyApp.Address', { | |
isRequired : YES, | |
isNested: YES, | |
defaultValue: function(record, key) { | |
return record.createNestedRecord(MyApp.Address); | |
} | |
}), | |
}); | |
At that point, instantiation using the dataStore framework would look something like this: | |
var myUser = MyApp.store.find(MyApp.User, 'my_app_unique_id'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment