Created
May 27, 2014 13:32
-
-
Save ushiboy/285e6f692447c918301e to your computer and use it in GitHub Desktop.
BackboneのModelに付け足す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
define([ | |
'underscore', | |
'backbone' | |
], function(_, Backbone) { | |
factory(root, _, Backbone); | |
}); | |
} else if (typeof exports !== 'undefined') { | |
var _ = require('underscore'), | |
Backbone = require('backbone'); | |
factory(root, _, Backbone); | |
} else { | |
factory(root, root._, root.Backbone); | |
} | |
}(this, function(root, _, Backbone) { | |
'use strict'; | |
Backbone.Model.prototype.with = function(fn, context) { | |
var attrs = _.clone(this.attributes); | |
fn.apply(context, [attrs]); | |
this.set(attrs); | |
return this; | |
}; | |
/** | |
* Usage: | |
*/ | |
// var User = Backbone.Model.extend({}), | |
// user = new User({ | |
// name : 'juck', | |
// age : 30 | |
// }); | |
// user.on('change', function() { console.log(arguments); }); | |
// user.with(function(a) { | |
// console.log(a); | |
// a.name = 'juck2'; | |
// a.age = 300; | |
// }); | |
return Backbone; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment