Skip to content

Instantly share code, notes, and snippets.

@agconti
Created August 5, 2014 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agconti/fa0933c2df1a1b7a990e to your computer and use it in GitHub Desktop.
Save agconti/fa0933c2df1a1b7a990e to your computer and use it in GitHub Desktop.
#trigger {margin: 5em;}
<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
$(function(){
var myModel = Backbone.Model.extend({});
var myCollection = Backbone.Collection.extend({
doStuff: function(){ alert("doing Stuff"); },
doOtherStuff: function(){ alert("doing other stuff"); }
});
var myCollectionView = Backbone.View.extend({
template: "<button id='trigger'>Trigger</button>",
initialize: function(){
this.listenTo(this.collection, 'sync', this.collection.doStuff);
this.listenTo(this.collection, 'sync', this.collection.doOtherStuff);
this.render();
},
events: {
"click #trigger": "trigger"
},
render: function(){
$("#app").html(this.template);
},
trigger: function(){
alert("triggering");
$(this).trigger("sync");
}
});
var mymodel = new myModel({}),
mycollection = new myCollection({
model: mymodel
}),
mycollectionview = new myCollectionView({
collection: mycollection
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment