Backbone-relational hasmany best practices

断了今生、忘了曾经 提交于 2019-12-05 01:08:02

Here's the solution that I have going on my current project. Note that Project hasMany comments, events, files, and videos. Those relations and their reverse relations are defined on those models:

Entities.Project = Backbone.RelationalModel.extend({
    updateRelation: function(relation) {
        var id = this.get('id'),
            collection = this.get(relation);

        return collection.fetch({ data: $.param({ project_id: id }) });
    }
});

I have the REST endpoint configured to take parameters that act as successive "WHERE" clauses. So project.updateRelation('comments') will send a request to /comments?project_id=4 I have some further logic on the server-side to filter out stuff the user has no right to see. (Laravel back-end, btw)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!