骨干JS - 减少对服务器的调用 [英] backbone js - reduce calls to the server

查看:84
本文介绍了骨干JS - 减少对服务器的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道人们处理如何停止多个外部服务器调用?我在的.complete千方百计的获取,否则当我尝试调用任何东西的获取还没有完成,没有什么是在收集填充。

Just wondering how people deal stopping multiple external server calls? I'm doing everything in the .complete of the fetch because otherwise when I try to call anything the fetch hasn't completed and nothing is populated in the collection.

我是新来的骨干,所以我可能缺少一个窍门..但有没有办法做了提取和存储的地方的信息,让您再也不用回来,你刚工作了集合为变量?我所有的信息都来自外部网站,所以如果我能我不想打很多不必要的外部呼叫。我不更新服务器或任何东西,它都只是只读的。

I'm new to backbone so I'm probably missing a trick.. but is there a way to do a fetch and store that information somewhere so that you never have to fetch again, you just work off the collection as a variable? All of my information comes from an external site, so I don't want to be making lots of unnecessary external calls if I can. I'm not updating the server or anything, its all just read-only.

什么其他人对类似的一组了呢?我失去了一些愚蠢的?还是我设置了很糟糕的呢?这里是我到目前为止(工作正在进行中)

What do other people do for a similar set up? Am I missing something silly? Or am I set up badly for this? Here's what I have so far (work in progress)

哦也:我做的获取在路由器..是一个坏主意。

Oh also: I'm doing the fetch in the router.. is that a bad idea?

http://jsfiddle.net/leapin_le$p$pchaun/b8y6L0rf/

.complete(
    //after the fetch has been completed
    function(){
        //create the initial buttons
        //pull the unique leagues out
        var uniqueLeagues = _.uniq(matches.pluck("league"));

        //pull the unique leagues out   
        var uniqueDates = _.uniq(matches.pluck("matchDate"));

        //pass to info to the relative functions to create buttons   
        getLeagues(uniqueLeagues);
        getMatchDates(uniqueDates);
        homeBtn();
        fetched = true;
    }
); //end complete

感谢您的时间!

推荐答案

这是一个经常重复出现的问题,但答案很简单。结果
也许我今天会做出一些图纸,如果它帮助。结果
我从来没有花时间去学习UML正常,所以请原谅我了点。

This is an often recurring question but the answer is rather simple.
Perhaps I'll make some drawings today, if it helps.
I never took the time to learn UML properly, so forgive me for that.

1。问题

什么您目前是这样的:

问题是但是,这是不是非常有活力。结果
如果在合适的这3个功能将需要从不同的AJAX回调函数执行时,它们需要被添加到任何这些回调。结果
试想一下,你想改变任何这些3函数的名称,就意味着你的code将打破瞬间,你会需要更新这些回调。

The problem however is that this isn't very dynamic.
If these 3 functions at the right would require to be executed from different ajax callback functions, they need to be added to any of these callbacks.
Imagine that you want to change the name of any of these 3 functions, it means that your code would break instantly, and you would need to update each of these callbacks.

您的问题表明你觉得你想避免每个函数分别执行异步调用,这的确是这样的,因为这造成了不必要的开销。

Your question indicates that you feel that you want to avoid every function to perform the async call separately, which is indeed the case because this creates unnecessary overhead.

2。事件聚合

该解决方案是实现事件驱动的方法,它的工作原理是这样的:

The solutions is to implement an event driven approach, which works like this:

此模式也被称为发布/订阅(或观察者模式),因为有发布事件(在这种情况下,左侧)对象和认购(右侧)的对象。结果
有了这个模式,你不需要调用Ajax回调结束后明确各功能;相反,对象订阅特定事件,并且当事件被触发执行方法。这样,你总是肯定的是,这些方法将被执行。结果
请注意,触发事件时,参数可以通过为好,它允许您从认购对象访问集合。

This pattern is also called pub/sub (or observer pattern) because there are objects that publish events (in this case on the left) and objects that subscribe (on the right).
With this pattern, you don't need to call every function explicitly after the ajax callback is finished; rather, the objects subscribe to certain events, and execute methods when the event gets triggered. This way you are always certain that the methods will be executed.
Note that when triggering an event, parameters can be passed as well, which allows you to access the collection from the subscribing objects.

3。骨干实施

主干促进事件驱动的方法。结果
设置事件聚合器是简单且可以做如下:

Backbone promotes an event driven approach.
Setting up an event aggregator is simple and can be done as follows:

window.APP = {};
APP.vent = _.extend({}, Backbone.Events);

从Ajax回调,你只需触发事件(你给它任何你想要的名字,但按照惯例,分号用作分隔符):

From the ajax callback, you just trigger an event (you give it any name you want, but by convention, a semi colon is used as a separator):

APP.vent.trigger("some:event", collection);  

的3个接收对象订阅事件如下:

The three receiving objects subscribe to the event as follows:

APP.vent.on("some:event", function(collection){ 
    console.log(collection.toJSON()); 
}); 

这就是基本上所有。结果
有一点要考虑到的是,以确保当您订阅使用开事件,您还需要取消订阅通过调用关,如果你不再需要的对象。

And that's basically all.
One thing to take into account is to make sure that when you subscribe to events using "on", you also need to un-subscribe by calling "off", if you no longer need the object.

这篇关于骨干JS - 减少对服务器的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆