Skip to content

Instantly share code, notes, and snippets.

@ushiboy
Created May 27, 2014 13:32
Show Gist options
  • Save ushiboy/285e6f692447c918301e to your computer and use it in GitHub Desktop.
Save ushiboy/285e6f692447c918301e to your computer and use it in GitHub Desktop.
BackboneのModelに付け足す
(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