我应该怎样声明的模式目前在A"取"状态,我应该如何创建"在获取完整的"事件绑定到? [英] How should I state that a model is currently in a "fetching" state and how should I create "on fetch complete" events to bind to?

查看:71
本文介绍了我应该怎样声明的模式目前在A"取"状态,我应该如何创建"在获取完整的"事件绑定到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这是关于Backbone.js的一个问题。这不是一个我怎么做到这一点之类的问题,因为我pretty相信我能创造我自己的,但我不知道我的解决办法是所有的干净。问题是多一点概念。我想了解,如果已经有一个约定要做到这一点,也许甚至在骨干网的事,但更普遍的。

Please note this is a question with regard to Backbone.js. This is not a "how do I do this" kind of question, as I'm pretty sure I can create my own, but I'm not sure my solution would be all that "clean". The problem is a bit more conceptual. I'm trying to understand if there's already a convention to do this, maybe not even in Backbone, but something more general.

这是的东西,我想处理状态。

This is the state of things that I'm trying to deal with.


    创建
  1. 查看,但相关的集合,虽然创建的,可能尚未初始化。视图需要一些code的模型初始化被执行。如果模型已经被初始化,即code可被立即执行。 (同样地,如果在模型没有被初始化,则code的以等待或被排队,直到模型进行初始化,)

  2. 的收集可以直接设置由重置调用或调用。

  1. View is created but a related collection, though created, may not be initialized yet. The view needs some code to be executed when the model is initialized. If the model is already initialized, that code can be executed immediately. (Likewise, if the model is not initialized, the code has to "wait" or "be queued up" until the model is initialized,)
  2. The collection may be directly set up by a reset invocation or a fetch invocation.

我在骨干实现,有我可以绑定到一个重置事件。但我只是想绑定到复位事件,如果重置尚未调用着呢,我只是想绑定到它曾经,所以我可能会想这样做,我绑定到重置事件东西,而事件中处理程序,解除对相同的处理。

I realize in Backbone, there is a reset event I can bind to. But I only want to bind to the reset event if reset hasn't been called yet, and I only want to bind to it "once", so I might want to do something where I bind to the reset event, and within the event handler, unbind that same handler.

我也想处理的情况下(可以称之为需求#3,虽然它不是严格需要我的地方在现在)的集合可能需要设置或获取超过一次它的数据多。

I'd also like to handle the case (lets call it requirement #3, although it's not strictly needed for where I'm at now) that the collection may need to be set or to fetch it's data more than once.

有人可以为我提供一些样本code满足上述两个要求(和preferably所有3所提到的)?或者至少,我讨论可能采取几种方法?谢谢你。

Can somebody provide me with some sample code that meets the two requirements above (and preferably all 3 mentioned)? Or at least discuss a few approaches I might take? Thank you.

编辑:

是的,这可能需要具体情况来形容。我可能还需要有人来告诉我,如果这只是气味腥。

Yes, this may need a specific circumstance to describe. I also might want somebody to tell me if this just smells fishy.

具体的情况是,我有一个路线,我有呈现一个选择控制一个观点,实际上另一种观点认为,可以称之为路由意见,告诉看法它控制如何导航以及如何重置自己从导航路线。我不知道,如果一个观点是很好的对于这一点,它只是似乎比code我有previously有这一切都散落在更好的地方。

The specific circumstance is, I have a route, and I have a view that renders a select control, and actually another view, lets call it a "routing view", that tells views it controls how to navigate and how to reset themselves from the navigated route. I don't know if a view is good for this, it just seemed better than the code I had previously had for this was strewn all over the place.

所以,说我进来作为一个用户,我有能力psented $ P $从选择视图和一些其他的东西选择的市场的。路由视图结合到更改事件的实际选择控制的,这样当用户改变选择控制,新的路由在

So, Say I come in as a user, and I am presented with the ability to select a market from the select view and a few other things. The routing view binds to the actual select control's on change event so that when the user changes the select control, a new route is navigated to, as in

http://localhost/SomePage.aspx#!/event-search/market/san-angelo/date/2012-01-04

(其它值增加,因为你可以看到。)

(with other values added, as you can see.)

路由视图还需要照顾从导航回到观点结合(或部件,因为他们),所以当URL有/日期/ 2012-01-04,我的日期选择器具有2012/1/4作为值,或者如果我的市场有/市场/圣诞老人-FE,选择将显示圣达菲。我还在路由基于所述当前路线进行搜索的能力设定。这也让我打后退按钮和前进按钮,看到我的previous搜索和控制将总是在导航发生的时间与我的初始选择。的非常酷

The routing view also takes care of binding from the navigation back to the views (or widgets as they were) so when the url has "/date/2012-01-04", my date picker has 1/4/2012 as the value, or if my market has "/market/santa-fe", the select would show "Santa Fe". I've also set up in the routing the ability to search based upon the current route. This also allows me to hit the back button and forward button and see my previous searches and the controls would always match my original selection at the time the navigation was made. Very cool.

在出现问题时,第一次加载页面。目前我选择控件绑定到一个模型,也恰好为网页(当然,这可能不是最好的主意,在另一种观点提供了数据,但既然我们已经使用在页面上深度数据的一些看法,这不是如此疯狂总是能借助的是,选择视图)。数据是通过AJAX获取和不可用在页面加载时。然而,路由视图的的页面加载时初始化。 因此,当它试图选择关于当前路由的(字面)选项,该选项甚至没有呈现呢。这将导致任何错误,它只是意味着,目前市场上没有选择在选择框。

The problem arises when the page first loads. Currently my select control is bound to a model which also happens to provide data for another view on the page (admittedly, this might not be the best idea, but since we already have some views using that data in depth on the page, it's not so insane to piggyback the select view off of that). That data is fetched via ajax and is not available when the page loads. However, the "routing view" is initialized on when the page loads. So when it tries to select the (literal) option pertaining to the current route, the option isn't even rendered yet. This results in no error, it just means that the current market isn't selected in the select box.

由于它目前为中,code的执行总是会发生在这个秩序,所以绑定到复位事件可能会奏效,但在未来,我可能被认为移动code左右,这样有一个机会模型实际上可能由路由器视图被初始化,也许加快页面加载或东西时收到的所有数据。因此,在这种情况下,我不能绑定重置,因为该模型的已经被重置的,我只是需要需要选择当前选择视图中的价值......也就是说,如果它已经被渲染。

As it stands currently, the code execution will always happen in this order, so binding to the reset event might work, but in the future, I might conceivably move code around such that there's a chance the model may actually have received all its data by the time the router view is initialized, maybe to speed up page load or something. So in that case, I cannot bind to reset because the model has already been reset and I just need need to select the value on the current select view... that is, if it's already been rendered.

人力资源管理模式。

有个声音告诉我,我该得太多错误的事情,underthinking正确的。

Something tells me I'm overthinking the "wrong" things and underthinking the "right" ones.

推荐答案

我已经打了一个类似的问题与集合。幸运地是,code段,而不是你寻找什么,可能有帮助。

I've hit a similar issue with collections. Hopefully the following code snippets, while not exactly what you're looking for, might help.

在收集,我钩到自己的重置事件,并将其标记为牵强:

In the collection, I hook into its own reset event, and mark it as fetched:

return Backbone.Collection.extend({

    initialize: function() {
        this.isFetched = false;
        this.bind('reset', this.onReset, this);
    },

    onReset: function() {
        this.isFetched = true;
    }

});

然后在视图中,我既可以使整个集合,或呈现正在载入消息:

Then in the view, I can either render the whole collection, or render a 'loading' message:

return Backbone.View.extend({

    render: function () {
        if (this.myCollection.isFetched) {
            this.myCollection.each(function (x) {
                // Render single item.
            });
        } else {
            // Set a loading animation.
        }
    }

});

显然认为需要挂接到集合的重置事件,并调用渲染()时,它触发

这篇关于我应该怎样声明的模式目前在A"取"状态,我应该如何创建"在获取完整的"事件绑定到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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