本文介绍了主干 - 执行多个取()渲染视图前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想是不是最好的模式/方法。这是我的路由器功能,让用户点击报价/:身份证',但对于这一观点来呈现,我需要他们的项目,客户和货币的列表。这将是确保所有3个取()试图实例化 quotesEdit 视图之前发生的最好方法?是它认为不好的做法,当用户点击的东西抓住所有的信息?

  quotesEdit:功能(ID){
        kf.Collections.quotes = kf.Collections.quotes ||新kf.Collections.Quotes();
        kf.Collections.projects = kf.Collections.projects ||新kf.Collections.Projects();
        kf.Collections.currencies = kf.Collections.currencies ||新kf.Collections.Currencies();
        //做一个取()为上述3
        kf.Collections.customers = kf.Collections.customers ||新kf.Collections.Customers();
        VAR报价= kf.Collections.quotes.where({ID形式:parseInt(ID,10)});
        kf.Utils.ViewManager.swap('sectionPrimary',新kf.Views.section({
          部分:quotesEdit',
          型号:报价[0]
        }));
    }


解决方案

我觉得方法解决了这个优雅的:

  //调用获取的三个集合,并信守承诺
VAR完整= _.invoke([行情,项目,货币],'取');//当所有的人都完成...
$ .when.apply($,完成).done(函数(){
   //一切准备就绪,并好去...
});

I was wondering about the best pattern/approach here. This is a function in my router, so the user hits 'quotes/:id', but for that view to render, I need a list of their projects, customers and currencies. What would be the best way to make sure all 3 fetches() have occurred before trying to instantiate the quotesEdit view? Is it considered bad practice to grab all the information when the user clicks something?

    quotesEdit: function(id) {
        kf.Collections.quotes = kf.Collections.quotes || new kf.Collections.Quotes();
        kf.Collections.projects = kf.Collections.projects || new kf.Collections.Projects();
        kf.Collections.currencies = kf.Collections.currencies || new kf.Collections.Currencies();
        //do a fetch() for the 3 above
        kf.Collections.customers = kf.Collections.customers || new kf.Collections.Customers();
        var quote = kf.Collections.quotes.where({Id: parseInt(id, 10)});
        kf.Utils.ViewManager.swap('sectionPrimary', new kf.Views.section({
          section: 'quotesEdit',
          model: quote[0]
        }));
    }
解决方案

I find a combination of jQuery deferreds and underscore's invoke method solves this elegantly:

//call fetch on the three collections, and keep their promises
var complete = _.invoke([quotes, projects, currencies], 'fetch');

//when all of them are complete...
$.when.apply($, complete).done(function() {
   //all ready and good to go...
});

这篇关于主干 - 执行多个取()渲染视图前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:31