Giter VIP home page Giter VIP logo

backbone.modelmorph's Introduction

#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

backbone.modelmorph's People

Contributors

disruptek avatar rhysbrettbowen avatar

Stargazers

Andrei Zharov avatar Abdi Haikal avatar  avatar e1024kb avatar Almog Melamed avatar  avatar Bernhard Gschwantner avatar Eric Hartford avatar Geert avatar  avatar Dario Agliottone avatar Jórgos Skettos avatar Stacey Reiman avatar J Wylie Кулик avatar  avatar justin talbott avatar Brandon Papworth avatar Jonathan Barratt avatar

Watchers

 avatar Eric Hartford avatar  avatar

backbone.modelmorph's Issues

not compatible with lodash

lodash _.extend behave differently when copying method attribute, so I patch in underscore extend. This is for user who are using frameworks like chaplin.js that come predefined with backbone and lodash

   };

   Backbone.Morph = function (model, schema) {
+
+
+    var old_extend = function (obj) {
+        _.each(_.toArray(arguments).slice(1), function(source) {
+          if (source) {
+            for (var prop in source) {
+              obj[prop] = source[prop];
+            }
+          }
+        });
+        return obj;
+    }
+
+
     // create a new Morph model that uses the original model as a prototype
     var Morph = function() {};
-    _.extend(Morph.prototype, model, proto);
+    old_extend(Morph.prototype, model, proto);
+
     var morph = new Morph();
     morph._schema = schema;
     morph._model = model;


not compatible with backbone 1.10

backbone 1.10 remove model.change() method so my patch is to call directly _model.set with the result of _schema[key].set

also support for 1 to 1 mapping where _schema[key].set return a value instead of an array

           return this._model.set(key, this._schema[key].set(value), options);
         // else we expect back an array of values to set each require
         var vals = this._schema[key].set(value);
-        _.extend(options, {silent: true});
-        _.each(vals, function(val, ind) {
-          this.set(this._schema[key].require[ind], val, options);
-        }, this);
-        this._model.change();
+        if (_.isArray(vals)) {
+            _.each(vals, function(val, ind) {
+              this._model.set(this._schema[key].require[ind], val, options);
+            }, this);
+        } else { // require only have one input, so one to one mapping
+            this._model.set(this._schema[key].require[0], vals, options);
+        }
+
       }
     },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.