English 中文(简体)
主干.js 从模型中的嵌套阵列中嵌入收藏集
原标题:backbone.js populating a collection from nested array in model

这里我想做的是: 我首先加载一个活跃网站用户列表, 每个用户都和电影列表相关。 当我点击一个用户时, 我希望他的电影列表出现在下一个菜单上 。 但是, 与其在最初的 JSON 请求中加载每个用户的完整电影列表, 不如用以主干关系包装的模型. fetch relate( “ 键 ” ) 选项来做一些懒惰的装入 。

我认为,我有一个活动听众:

events: {
     click .username :  showMovies 
},
showMovies: function(event){
    event.preventDefault();
    this.model.select();
}

在我的模型中,我有一个选择的函数:

select: function(){
    this.set({selected: true});
    this.fetchRelated("movies");
    var usermovies = new UserMovieCollection(); 
    usermovies.add(this.get("movies"));     
}

问题在于这个. fetchelate( “ movies ” ) 是一个不同步的事件, 所以当加号被调用时, 电影实际上并没有加载。 所以我想知道我在这里有什么选择。 我有一个想法是将它包在jQuery 的推迟对象中... 基本上, 使用回调 。 我不太确定如何做到这一点, 但是, 我不知道使用回调形式是否真的好 。

有人知道我怎样才能执行这个计划吗? 要么是使用回调,要么是某种(预设的)系统?谢谢!

编辑:

我设法用延后的办法,但我仍然在想,是否有一个更好的支柱解决办法,而不延后。

select: function(){
        var model = this; 
        var usermovies = new UserMovieCollection();
        $.when(this.fetchRelated("movies")).then(function(){
            console.log(model.get("movies"));
        });
    }
问题回答

从医生那里看一看, 似乎可能有一个事件 你可以绑在一起吗?

var user = new User({ name:  User 1  } );
user.bind(  add:movies , function( model, coll ) {
    // Maybe this will work?
});

我对后骨不太熟悉

祝你好运!





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签