Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Attributes Mixin (#2526) #2557

Closed

Conversation

matthewwithanm
Copy link

As suggested by @tgriesser, I've implemented the feature I suggested in #2526 (a mixin for adding observable attributes to arbitrary objects). It works just like Backbone.Events, in that it's used with _.extend. There's not much new here; just making the related methods usable on non-Models.

var object = {};
_.extend(object, Backbone.Attributes);
object.on('change', function(){ alert('changed'); });
object.set('someAttribute', 'value');

Or on a view:

class TinyWidget extends Backbone.View
    _.extend @prototype, Backbone.Attributes

widget = new TinyWidget
widget.on 'change:color', -> alert 'color changed!'
widget.set color: '#0000FF'

@jashkenas
Copy link
Owner

Very cool refactor -- but I think it's a bit too granular and slice-and-dicey for us. 95% of Backbone users probably won't encounter a need for this mixin where simply using a model wouldn't be easier. Makes for a nice extraction or plugin, though.

@jashkenas jashkenas closed this May 23, 2013
@matthewwithanm
Copy link
Author

I understand. Thanks for taking a look!

@mateusmaso
Copy link

@matthewwithanm I really liked the idea and it would fit very nicely for people who wants the same behavior of attributes for Collections (meta) and Views (options).. I don't think that everything must be a model to hold attributes (models have much more meaning), but making it granular only brings reusability to not reinvent the wheel for get, set and change events..

@jashkenas In general, Backbone should care more about separating concerns into modules instead of guessing how many users would use it.. I like the simplicity of this lib, but it should be also simple to extend and reuse functionalities for framework purposes.

@akre54
Copy link
Collaborator

akre54 commented Aug 4, 2013

Here's a simple script that pulls out @matthewwithanm's changes into a standalone plugin if you so choose:

Backbone.Attributes = _.extend({}, Backbone.Events);
var modelMethods = ['get', 'set', 'unset', 'clear', 'has', 'changed', 'hasChanged', 'changedAttributes', 'previous', 'previousAttributes'];
var wrappedMethods = ['get', 'set', 'clear', 'changedAttributes'];

_.each(modelMethods, function(method) {
  var fn = Backbone.Model.prototype[method];
  Backbone.Attributes[method] = _.contains(wrappedMethods, method) ?
     function() { this.attributes || (this.attributes = {}); return fn.apply(this, arguments) } : fn;
});

Backbone.Attributes._validate = function() { return true; }; // hacky, but works


// Later
var view = new Backbone.View;
_.extend(view, Backbone.Attributes);
view.set(...);

EDIT: Now available as a full-blown plugin: https://github.com/akre54/backbone.attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants