简体   繁体   English

与ribs.js的多对多关系事件

[英]Many to many relationship events with backbone.js

I have a many-to-many relationship with two of my backbone.js models implemented using a pivot table on the serverside. 我与使用服务器端的数据透视表实现的两个bone.js模型具有多对多关系。 I'm trying to figure out how to structure it clientside. 我试图弄清楚如何在客户端构建它。 My current structure is: 我当前的结构是:
1) I have a Tag model, and a TagView which renders a checkbox and the tag label, I have a checked event on the checkbox which does nothing at the moment. 1)我有一个Tag模型,还有一个TagView和一个呈现复选框和标签标签的TagView,我在复选框上有一个checked事件,该事件目前不起作用。

  • I have a TagsCollection, which holds a bunch of Tags. 我有一个TagsCollection,其中包含一堆标签。
  • I have a TagsCollectionView, which binds add, reset etc of the TagsCollection, and adds TagViews for the added Tags, renders them, and appends the html to its current html (on reset, the html is reset). 我有一个TagsCollectionView,它绑定TagsCollection的添加,重置等,并为添加的Tags添加TagViews,呈现它们,并将html附加到其当前html(重置时,重置html)。
  • I have a global TagCollection instance which contains all the possible tags 我有一个全局TagCollection实例,其中包含所有可能的标签
  • I have a Notes Model which contains an (empty) TagCollection called selectedtags on init. 我有一个Notes模型,其中包含一个(空)TagCollection,在初始状态下称为selectedtags。
  • The server returns an array of selected tagids for each Notes, which I add to its TagCollection. 服务器为每个Notes返回一个选定的tagid数组,我将其添加到其TagCollection中。
  • Now comes the hard part, tying it all together.. my NotesView has its own TagsCollectionView which is bound to the global TagsCollection (so it can list all the Tags).. now, how do I get a checked event on the checkedbox of its sub TagViews to trigger an add to this Notes model's selectedtags? 现在是最困难的部分,将它们捆绑在一起。.我的NotesView有自己的TagsCollectionView,它绑定到全局TagsCollection(因此它可以列出所有Tag)。.现在,如何在其复选框上选中事件sub TagViews触发添加到此 Notes模型的selectedtags? Should I provide a reference to the this Notes model instance to the TagsCollectionView on init which then provides it to all the TagViews it creates, whose checked event then adds/removes items from that model? 我是否应该为init上的TagCollectionView提供此Notes模型实例的引用,然后将其提供给它创建的所有TagView,然后将其选中的事件从该模型中添加/删除项目? That's the best way I can figure out how to do this, any other thoughts would be appreciated. 这是我弄清楚如何执行此操作的最佳方法,任何其他想法都将不胜感激。

View is only for visual display of model. 视图仅用于模型的可视化显示。 Please specify the need for TagsCollectionView in more details: 请更详细地指定对TagsCollectionView的需求:

  1. Use change event for checking the checkbox. 使用更改事件来检查复选框。
  2. I would advise incremental coding. 我建议增量编码。 First work with the Tag and TagView, As it works, continue adding collection to hold the Tags. 首先使用Tag和TagView,在工作时,继续添加收藏夹来保存标签。 After that you can add Notes. 之后,您可以添加注释。 It's a 'divide and conquer' :) 这是“分而治之” :)
  3. Don't confuse with the whole design, it is very simple when you start. 不要混淆整个设计,开始时非常简单。

I can not provide the whole design due to lack of requirement details. 由于缺少需求细节,我无法提供整个设计。 but, I think below code should trigger the starting point of your design. 但是,我认为下面的代码应该触发设计的起点。

var TagsCollectionView=Backbone.View.extend({
 el:$(), 
});

var Tag=Backbone.Model.extend({
  defaults:{
   // define the default properties
  }
});

var TagView=Backbone.View.extend({
  el:$("body"),
  events:{
   'change #checkBox':'customFunction'
  },
  initialize: function(){
   _.bindAll(this, 'render','customFunction');
   this.render();   
  },  
  render:function(){
   var tag=new Tag();
   // code to render the checkbox and label
  },
  customFunction:function(){
   // whatever you want to do after checking event of checkbox
  }
});

// collection to collect all the tags
var TagCollection=Backbone.Collection.extend({
  model:Tag
});

var Notes=Backbone.Model.extend({
  defaults:{
   'tagCollection':TagCollection
  }
});

// you do not require TagsCollectionView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM