Skip to content

Instantly share code, notes, and snippets.

@igkuz
Last active December 11, 2015 20:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igkuz/4653259 to your computer and use it in GitHub Desktop.
Save igkuz/4653259 to your computer and use it in GitHub Desktop.
var Comments = Backbone.Collection.extend({
url: function() {
return this.baseUrl + '/comments';
},
initialize: function(bootstrapData, options) {
this.baseUrl = options.baseUrl;
}
}),
Posts = Backbone.Collection.extend({
model: Backbone.Model.extend({
url: '/api/posts',
defaults: {
id: null,
title: '',
body: '',
description: ''
},
initialize: function() {
this.comments = new Comments(null, {
baseUrl: this.url + "/" + this.id
});
// Подписываем событие на то что мы загрузили основной текст поста. Как только мы это сделали
// запускаем загрузку коментариев.
this.on('change:body', function() {
this.comments.fetch();
}, this);
}
}),
getPost: function(id) {
// Получаем модель
var post = this.get(id);
// Если модель нашли, то говорим ей загрузить полные данные и коментарии
if (post) {
post.fetch();
}
}
}),
posts = new Posts;
// Загружаем все посты
posts.fetch();
// Когда понадобиться показать какой-то пост то вызываем
posts.getPost('id-xxx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment