Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created April 27, 2017 12:25
Show Gist options
  • Save absent1706/5cab57c8e350d1b3bc109c817f315467 to your computer and use it in GitHub Desktop.
Save absent1706/5cab57c8e350d1b3bc109c817f315467 to your computer and use it in GitHub Desktop.
ko init observable array
/* Variants of initialization */
/* 1 */
self.emails = ko.mapping.fromJS(emailArray);
/* 2 */
self.emails = ko.observableArray();
ko.mapping.fromJS(emailArray, undefined, self.emails);
/* 3 */
self.emails = ko.observableArray();
emailArray.forEach(function(data, index, array) {
var email = new UserEmail(data);
self.emails.push(email);
});
/* 4 */
function UserEmail(data) {
// koMapping.fromJS(data, {}, this);
var self = this;
self.id = ko.observable(data.id);
self.user_id = ko.observable(data.user_id);
self.email = ko.observable(data.email);
}
var mapping = {create: function(options) {
return new UserEmail(options.data);
}};
ko.mapping.fromJS(emailArray, mapping, self.emails);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment