Last active
December 11, 2015 20:09
-
-
Save igkuz/4653259 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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