Created
January 11, 2014 15:42
-
-
Save 1ambda/8372408 to your computer and use it in GitHub Desktop.
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
window.Instance = Backbone.Model.extend({ | |
}); | |
window.Instances = Backbone.Collection.extend({ | |
model : Instance | |
}); | |
window.InstanceItem = Backbone.View.extend({ | |
tagName : 'tr', | |
template : _.template($('#tmpl_instance_item').html()), | |
events : { | |
'click' : 'itemClick' | |
}, | |
render : function() { | |
var tmpl = this.template(this.model.toJSON()); | |
$(this.el).html(tmpl); | |
return this; | |
}, | |
itemClick : function() { | |
alert(JSON.stringify(this.model.toJSON())); | |
} | |
}); | |
window.InstanceList = Backbone.View.extend({ | |
el : $('#target'), | |
template : _.template($('#tmpl_instance_list').html()), | |
initialize : function() { | |
this.listenTo(this.collection, 'add', this.addOne); | |
this.listenTo(this.collection, 'reset', this.addAll); | |
this.views = []; | |
}, | |
render : function() { | |
var tmpl = this.template(); | |
$(this.el).html(tmpl); | |
return this; | |
}, | |
addOne : function(item) { | |
var view = new InstanceItem({ | |
model : item, | |
}); | |
this.views.push(view); | |
this.$('tbody').append(view.render().el); | |
}, | |
addAll : function() { | |
console.log('addAll'); | |
this.removeAll(); | |
this.collection.each(this.addOne, this); | |
}, | |
removeAll : function() { | |
console.log(this.views.length); | |
if (this.views.length) { | |
_.each(this.views, function(item) { | |
item.remove(); | |
}); | |
this.views.length = 0; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment