Skip to content

Instantly share code, notes, and snippets.

@kaineer
Created January 28, 2012 08:06
Show Gist options
  • Save kaineer/1693380 to your computer and use it in GitHub Desktop.
Save kaineer/1693380 to your computer and use it in GitHub Desktop.
Ура, заработало!
Backbone.Bindings = {
setModel: function(model, bindings) {
var view = this;
bindings || (bindings = (this.bindings || {}));
if(this.model) {
this.model.unbind();
}
_.each(bindings, function(i, selector) {
var property = bindings[selector];
model.bind("change:" + property, function() {
view.$(selector).val(model.get(property));
});
view.$(selector).unbind("change");
view.$(selector).bind("change", function(event) {
var $this = $(event.target);
var hash = {};
hash[property] = $this.val();
model.set(hash);
});
});
this.model = model;
}
};
var BindedView = Backbone.View.extend(Backbone.Bindings);
var TheModel = Backbone.Model.extend({
});
var TheView = BindedView.extend({
el: "div#target",
bindings: {"#name": "name", "#weight": "weight"},
render: function() {
$(this.el).html("<input id='name' /><br/><input id='weight'/>");
}
});
var view = new TheView();
var model = new TheModel();
view.render();
view.setModel(model);
model.set({name: "Hello", weight: 100});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment