Models in XO are just plain Javascript objects with some functions attached.
var ModelDefinition = xo.model.extend({ ... })
Create a new model definition, extend the existing model definition from XO. THis model can then be extended further, much like classes.
var ModelDefinition = xo.model.extend({ ... })
Create a new model definition, extend the existing model definition from XO. THis model can then be extended further, much like classes.
extend var ModelDefinition = xo.model.extend({ ... })
Create a new model definition, extend the existing model definition from XO. THis model can then be extended further, much like classes.
var Book = xo.model.extend({
initialize : function(){
console.log('New book!');
return this;
}
});
var model = ModelDefinition.create([attributes])
Creates a new instance of a model. An object attributes can be passed into to set up the model with those attributes.
model.initialize
Initialize is called whenever a new model is created using model.create(...)
. Any parameters passed into the create called are passed into this function.
model.set(attribute, val), model.set(attributes)
Sets attributes (one or many) to the Model. If any of them change the model's state, it will fire a change event, eg. change:title
. Any changes will also fire a single change
event for the model as well. You can also just set the attributes of a model using standard Javascript, eg. model.title = "Test"
, this is valid, but will not trigger any change events.
note.set({title: "March 20", content: "In his eyes she eclipses..."});
book.set("title", "A Scandal in Bohemia");
model.onChange(attribute, function)
onChange will set a change event listener for that attribute with the given function, as well as immeadiately call the function with the current value of the attribute. This is useful for setting up events on models that you are unsure if they've been populated yet.
model.toJSON()
Returns a copy of all the attributes on the object as a new JSON object.