主干网使用现有模型数据,无需再次调用服务器

发布于 2024-12-22 06:13:24 字数 1202 浏览 0 评论 0原文

我是骨干网络的新手,我认为最棒的事情之一是我可以访问数据而无需返回服务器。在我的应用程序的旧版本中,我使用 jQuery.data 将数据哈希存储在 dom 节点中。

所以我有一个消息列表,当我单击一条消息时,我想转到一个显示更多详细信息的视图,但我需要的所有详细信息已经在原始列表中,所以我实际上不需要另一个运行到服务器,我只想更新视图。

然后,当我想返回到列表时,我不需要返回到服务器,我只想返回到原始列表。

我的路线如下所示,我尝试将“messages”变量传递到 show 函数中,但该函数无法使用它。

我是否误解了骨干的能力?

App.Routers.Messages = Backbone.Router.extend({
    routes: {
        "":             "index",
        "messages/:id":     "show"
    },

    show: function(id){
        var message = new Message({ id: id});
        message.fetch({
            success: function(model, resp){

                new App.Views.Show({ model: message});
            },
            error: function(){
                new Error({message: "Hey!? Were'd it go? sorry I can't find your message"});
                window.location.hash = '#';
            }
        });
    },

    index: function(){

        var messages = new App.Collections.Messages();
        message.fetch({
            success: function(){

                  new App.Views.Index(messages);

            },
            error: function(){
                new Error({ message: "did not find message :("});
            }
        });


    }

I'm new to backbone and I thought one of the great things was that I could access the data without going back to the server. In the old version of my app, I was storing a hash of data in a dom node using jQuery.data.

So I have a list of messages, and when I click on a message, I want to go to a view which shows me more details, but all the details I need are already in the original list, so I don't actually need another run to the server, I just want to update the view.

Then when I want to go back to the list, again, I don't need to go back to the server, I just want to go back to the original list.

My routes look like this, and I tried to pass the 'messages' variable into the show function, but it isn't available to that function.

Am I misunderstanding the capabilities of backbone?

App.Routers.Messages = Backbone.Router.extend({
    routes: {
        "":             "index",
        "messages/:id":     "show"
    },

    show: function(id){
        var message = new Message({ id: id});
        message.fetch({
            success: function(model, resp){

                new App.Views.Show({ model: message});
            },
            error: function(){
                new Error({message: "Hey!? Were'd it go? sorry I can't find your message"});
                window.location.hash = '#';
            }
        });
    },

    index: function(){

        var messages = new App.Collections.Messages();
        message.fetch({
            success: function(){

                  new App.Views.Index(messages);

            },
            error: function(){
                new Error({ message: "did not find message :("});
            }
        });


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2024-12-29 06:13:24

您也可以使用 Backbone 做您想做的事情。

您需要做的唯一更改是将模型集合设置为全局可访问变量。

所以在索引函数中替换

var messages = new App.Collections.Messages();

App.messages = new App.Collections.Messages();

然后在你的显示函数中替换

var message = new Message({ id: id});

var message = App.messages.get(id);

You can do what you desire with Backbone as well.

The only change you have to make is to set the collection of models to a global accessible variable.

So in the index function replace

var messages = new App.Collections.Messages();

with

App.messages = new App.Collections.Messages();

And then in your show function replace

var message = new Message({ id: id});

with

var message = App.messages.get(id);
寒江雪… 2024-12-29 06:13:24

问题是路由器的工作原理与此完全相同:路由,一旦您单击 messages/:id 视图,它就会重新初始化消息模型等。如果您将获取移动到您的初始化方法查看并返回该视图作为所谓的单例(例如,仅在应用程序中初始化一次),您不需要进行另一次服务器调用。

The problem is that the router works exactly like that: a route, once you click on the messages/:id view it reinitializes a new Messages model etc. If you move the fetch to the initialize method of your View and return that View as a so-called singleton (e.g. initialize it only once in your application), you won't need to do another server-call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文