Skip to content

Instantly share code, notes, and snippets.

@stolksdorf
Last active August 29, 2015 13:56
Show Gist options
  • Save stolksdorf/8790516 to your computer and use it in GitHub Desktop.
Save stolksdorf/8790516 to your computer and use it in GitHub Desktop.

Models

Models in XO are just plain Javascript objects with some functions attached.

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.

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.

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;
	}
});

create

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.

initialize

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.

set

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");

onChange

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.

toJSON

model.toJSON() Returns a copy of all the attributes on the object as a new JSON object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment