Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created November 9, 2011 14:53
Show Gist options
  • Select an option

  • Save mxriverlynn/1351655 to your computer and use it in GitHub Desktop.

Select an option

Save mxriverlynn/1351655 to your computer and use it in GitHub Desktop.
Backbone, Object Literals, View Events, jQuery and `el`
<div id="someElement">
<button id="someButton">Click Me</button>
</div>
MyView = Backbone.View.extend({
el: $("#someElement"),
events: {
"click #someButton": "clicked"
},
clicked: function(e){
e.preventDefault();
alert("I was clicked!");
}
});
var myViewDefinition = {
el: $("#someElement"),
events: {
"click #someButton": "clicked"
},
clicked: function(e){
e.preventDefault();
alert("I was clicked!");
}
};
MyView = Backbone.View.extend(myViewDefinition);
myObj = {
foo: "some value",
bar: 1 + 1
};
function add(a, b){
return a + b;
}
myObj = {
bar: add(1, 1)
}
$(document).ready(function(){
// your code here
});
$(function(){
// your code here
});
$(function(){
MyView = Backbone.View.extend({
el: $("#someElement"),
events: {
"click #someButton": "clicked"
},
clicked: function(e){
e.preventDefault();
alert("I was clicked!");
}
});
});
MyView = Backbone.View.extend({
events: {
"click #someButton": "clicked"
},
initialize: function(){
this.el = $("#someElement");
},
clicked: function(e){
e.preventDefault();
alert("I was clicked!");
}
});
$(function(){
var myView = new MyView();
});
MyView = Backbone.View.extend({
events: {
"click #someButton": "clicked"
},
clicked: function(e){
e.preventDefault();
alert("I was clicked!");
}
});
$(function(){
var myView = new MyView({
el: $("#someElement");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment