Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用函数管理主干网中的关系_Javascript_Backbone.js_Backbone Relational - Fatal编程技术网

Javascript 使用函数管理主干网中的关系

Javascript 使用函数管理主干网中的关系,javascript,backbone.js,backbone-relational,Javascript,Backbone.js,Backbone Relational,我正在(第一次)使用Backbone.js创建一个相当简单的应用程序。我目前与两个具有一对多关系的实体合作: 项目:有许多小组 组:属于一个项目 我研究了几种表示模型之间关系的不同方法,从主干网自己的建议到连接到主干网的关系。然而,我最终决定使用简单的函数调用。例如,在我的“项目”模型中,有一个名为groups()的函数: 组:函数(){ 返回新的Collections.Groups(Groups.where({project_id:this.get('id')})); } 在集团模式中:

我正在(第一次)使用Backbone.js创建一个相当简单的应用程序。我目前与两个具有一对多关系的实体合作:

  • 项目:有许多小组
  • 组:属于一个项目
我研究了几种表示模型之间关系的不同方法,从主干网自己的建议到连接到主干网的关系。然而,我最终决定使用简单的函数调用。例如,在我的“项目”模型中,有一个名为groups()的函数:

组:函数(){
返回新的Collections.Groups(Groups.where({project_id:this.get('id')}));
}
在集团模式中:

这让我可以做以下事情:

对我来说,这里的折衷是维护最新关系的开销(当使用主干关系时)与每次需要获取项目组时调用“groups()”函数所需的处理时间

在我以这种方式建立更多关系的过程中,是否有人能就我可能遇到的任何陷阱向我提供建议,或者推荐更好的解决方案(除了上面提到的扩展)?该应用程序最终将有大约6个实体,具有各种一对多和多对多关系

Project.groups 我有一个我称之为
AutoUpdatedCollection
的实现

我在场景中使用它,您有一个共享集合,其中包含一个类的每个模型,该集合用于填充子集合,这些子集合在更新共享集合时必须更新

检查系统的执行情况

根据您的示例,我假设有一个名为
window.groups
的公共/共享集合,然后我将实现
Project
模型,如下所示:

// code simplified and no tested
var Project = Backbone.Model.extend({
  initialize: function(){
    this.groups =
      new App.AutoUpdatedCollection({
        name:             "Project-" + this.id + "-groups",
        sourceCollection: window.groups,
        filterField:      "project_id",
        filterValue:      this.id
      }).filteredCollection;

    this.groups.on( "add", this.addGroup, this );
    this.groups.on( "remove", this.removeGroup, this );
  }
});
也许不是这么简单,如果你有任何问题,请告诉我

集团项目 我只需要初始化
Group.initialize()
中的
Group.project
属性:


我想我们在同一条船上;花了几个小时来寻找处理模型之间层次结构的合理且最简单的方法。我希望有人能在这里回答。你找到你最喜欢的经时间验证的解决方案了吗?我目前正在使用一种写在这里的方法:。它覆盖了“set”,使处理简单的自上而下关系变得非常容易
var foo = new Project();
var foo_groups = foo.groups();
foo_groups.at(0).get('name');

var bar = new Group();
var bar_project = bar.project();
bar_project.get('name');
// code simplified and no tested
var Project = Backbone.Model.extend({
  initialize: function(){
    this.groups =
      new App.AutoUpdatedCollection({
        name:             "Project-" + this.id + "-groups",
        sourceCollection: window.groups,
        filterField:      "project_id",
        filterValue:      this.id
      }).filteredCollection;

    this.groups.on( "add", this.addGroup, this );
    this.groups.on( "remove", this.removeGroup, this );
  }
});
var Group = Backbone.Model.extend({
  this.project = window.projects.where({id: this.get('project_id')})[0];
});