2

Edited This Below

In this image below I have two main regions.

One for the user list on the left: allusersRegion

And another for the the right side where a layout is displayed, which contains unique attributes to the user that was clicked in the allusersRegion and a list of articles by the user: middleCoreRegion

**If you noticed the middleCoreRegion is showing all articles by all users..This is wrong and I am trying to show all articles of the individual user (in this case. "kev")

enter image description here

I tried to see if my problem was with my JSON api (served via node/rest/mongoose) or with my underscore templates, but if it displays both list then I suppose I need to filter from inside backbone.

At first I tried using a Marionette.vent to simply change the url, but somhow I can't get the _id name into the url: function(), it says undefined...

var someuser = this.model.get("_id"); 
myApp.vent.trigger("showarticles", someuser);

I add a listener in the backbone collection on the same page:

myApp.vent.on("showarticles", someuser);

**The Edit (A Different Way of Doing this) Here is my code

var usertab = Poplive.module('usertab', {
    startWithParent: true,       
});

usertab.addInitializer(function() {

User = Backbone.Model.extend({});
UniqueArticle = Backbone.Model.extend({});

//Collections
Users = Backbone.Collection.extend({
model: User,
url: '/api/user2'
});

UniqueArticles = Backbone.Collection.extend({
  model: UniqueArticle,
url:  '/api/survey'
});

//Layout
var VisitingLayoutView = Backbone.Marionette.Layout.extend({
  template: "#visiting-layout",
  regions: {
    firstRegion: "#listone",
    secondRegion: "#listtwo",
    thirdRegion: "#listthree",
    playRegion: "#playhere",
    articlesRegion: "#articleshere" 
  }
});

AllUserView = Backbone.Marionette.ItemView.extend({
template: "#tab-alluser-template",
tagName: 'li',
events:  {
  "click #openprofile" : "OpenProfile"
},

OpenProfile: function(){
  console.log("Profile is open for " + this.model.get("username"));
  var modelo = this.model.get("_id");
  var vlv = new VisitingLayoutView({model: this.model});
  Poplive.middleCoreRegion.show(vlv);

var ua = new UniqueArticles();
var uacoll = new UniqueArticlesView({collection: ua});
vlv.articlesRegion.show(uacoll);
}
})

//ItemViews
UniqueArticleView = Backbone.Marionette.ItemView.extend({
template: "#unique-article-template"
});

//CollectionViews
AllUsersView = Backbone.Marionette.CompositeView.extend({
   template: "#tab-allusers-template",
   itemView: AllUserView
});

UniqueArticlesView = Backbone.Marionette.CollectionView.extend({
  template: "#unique-articles-template",
  itemView: UniqueArticleView
});

//Render Views
var alluserview = new AllUserView();
var allusersview = new AllUsersView();

//Fetch Collections
var theusers = new Users();
theusers.fetch();
var userscoll = new AllUsersView({collection: theusers});
Poplive.allusersRegion.show(userscoll);
           });
4

2 回答 2

3

我认为你想要的是定义url为一个函数,并user在你的集合上有一个属性:

var UniqueArticles = Backbone.Collection.extend({
  model: Article,

  initialize: function(){
    var self = this;
    myApp.vent.on("showarticles", function(someuser){
        self.user = someuser;
        self.fetch();
    }
  },

  url : function(){
      var fragment = '/api/visitingarticles/';
      if(this.user && this.user.id){
        return fragment + this.user.id;
      }
      return fragment;
  }
});

(免责声明:未经测试的代码,但它在我的脑海中有效:D)

然后每次触发事件时,user都会更新属性,并使用更新的 url 重置集合。

作为旁注,您可能想研究使用过滤的集合。我已经在我的书中实现了这个想法,基于 Derick Bailey 的代码:http: //jsfiddle.net/derickbailey/7tvzF/

这是我的版本:https ://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js

及其使用示例(第 38-41 行):https ://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L38

于 2013-06-07T06:59:58.573 回答
3

假设 UniqueArticle 是主干模型,对于要获取具有特定 id 的模型,您需要定义 urlRoot 属性,它将id模型的附加到请求中。

因此,当您对其进行提取时,id 属性将附加到来自服务器的模型请求的末尾

  var UniqueArticle = Backbone.Model.extend({
      idAttribute : 'someuser',
      urlRoot : function(someuser){
          return '/api/visitingarticles/'
      }
       // this would send a request for 
        //  /api/visitingarticles/someId
    });

    var UniqueArticles = Backbone.Collection.extend({
      model: Article,
      url : function(someuser){
          return '/api/visitingarticles/'
      }
      //  /api/visitingarticles   -- All Articles will be fetched
    });
于 2013-06-06T19:11:06.887 回答