Skip to content

rhysbrettbowen/Backbone.ModelMorph

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 

#Model Morph

Allows the definition of a schema that will create a new model with computed properties to be created from another model. Useful for things like rivets where we want to provide computed properties that are dynamic and will change dependant on the original model.

var orig = new Backbone.Model({
	firstName: 'fred',
	lastName: 'smith',
	title: 'Mr'
});

var morph = new Backbone.Morph(orig, {
	fullName: {
		get: function(first, last) {
			return first + ' ' + last;
		},
		set: function(name) {
			// return array matches require array to set
			return name.split(' ');
		},
		require: ['firstName', 'lastName']
	},
	titledName: {
		get: function(title, name) {
			return title + ' ' + name;
		},
		set: function(name) {
			return [name.substring(0,2), name.substring(3)];
		},
		// can use computed properties to compute more properties
		require: ['title', 'fullName']
	}
});

// can get from schema
morph.get('fullName'); // 'fred smith'

// or original values
morh.get('firstName'); // 'fred'

// can listen to changes on computed values
morph.on('change:fullName', function(model, name, options) {
	alert(name);
});

orig.set('firstName', 'bert'); // alert 'bert smith'

// can even set back through computed values
morph.set('edward pumpernickel'); // alert 'edward pumpernickel'

// and changes are made back to original model
orig.get('firstName'); // edward

#Changelog

##v1.0.0

-initial versioning

About

create a model of computed properties from another backbone model

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published