0
Я хотел бы извиниться за беспорядок внизу - с некоторыми трудностями со следующим кодом. Попытка отобразить представление каждой модели в представлении коллекции, которое не является кратким. Любая помощь будет принята с благодарностью. Советы и указатели. Заранее спасибо.Backbone.js Collection View Through Model View
$(function() {
/* Model */
var Publication = Backbone.Model.extend({
defaults: {
title: "",
published: ""
}
});
/* Collection */
var PublicationCollection = Backbone.Collection.extend({
model: Publication,
url: 'http://www.stellarbiotechnologies.com/media/press-releases/json'
});
/* Model View */
var PublicationView = Backbone.View.extend({
tagName: 'li',
className: 'publication',
el: 'displayHere',
template: _.template($('#publicationTemplate').html()),
initialize: function() {
this.model.on('destroy', this.remove, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
/* Collection View */
var AppView = Backbone.View.extend({
tagName: 'ul',
initialize: function() {
var pubs = this.collection;
pubs.fetch;
pubs.bind('reset', this.render);
pubs.bind('add', this.add, this);
pubs.bind('remove', this.remove, this);
},
render : function() {
this.collection.each(this.add, this);
return this;
},
add: function(pub) {
var pub = new PublicationView({model: Publication});
this.$el.html(pub.render().el);
},
remove: function(pub) {
var pubs = this.collection;
pubs.remove(pub);
pubs.render();
},
});
var App = new AppView({collection: PublicationCollection});
});
HTML:
<body>
<ul id="displayHere"></ul>
</body>
Шаблон:
<script id="publicationTemplate" type="text/template">
<td class="id"><%= id %></td>
<td class="title"><%= title %></td>
<td class="published"><%= published %></td>
</script>
Не удается получить эту работу благодаря –
Ах Nevermind он работает отлично спасибо ! –