Created
July 29, 2011 17:05
-
-
Save mxriverlynn/1114226 to your computer and use it in GitHub Desktop.
knockout-style data-bind attributes for backbone.js
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
var DataBindConvention = { | |
selector: "*[data-bind]", | |
handler: { | |
bind: function(selector, view, model){ | |
view.$(selector).each(function(index){ | |
var element = view.$(this); | |
var databind = element.attr("data-bind").split(" "); | |
var elementAttr = databind[0]; | |
var modelAttr = databind[1]; | |
model.bind("change:" + modelAttr, function(changedModel, val){ | |
switch(elementAttr){ | |
case "html": | |
element.html(val); | |
break; | |
case "text": | |
element.text(val); | |
break; | |
default: | |
element.attr(elementAttr, val); | |
} | |
}); | |
}); | |
} | |
} | |
}; | |
Backbone.ModelBinding.Conventions.dataBind = DataBindConvention; |
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
var vent = _.extend({}, Backbone.Events); | |
AddEditView = Backbone.View.extend({ | |
events: { | |
"click #save": "save" | |
} | |
initialize: function(options){ | |
this.vent = options.vent; | |
}, | |
save: function(){ | |
this.model.save(); | |
this.vent.trigger("modelUpdated"); | |
} | |
}); | |
MedicationView = Backbone.View.extend({ | |
initialize: function(options){ | |
_.bindAll(this, "render"); | |
options.vent.bind("modelUpdated", this.render); | |
}, | |
render: function(){ | |
// ... jQuery template stuff here | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment