使用 Backbone.Router 进行幻灯片放映

发布于 2024-12-15 20:23:53 字数 1815 浏览 1 评论 0原文

我正在使用 Backbone.js 创建幻灯片。我的幻灯片视图已完成,每张幻灯片都是一个模型,所有模型都在一个集合中。现在我想对我的幻灯片应用一点 hashbang 魔法:-)

这是我的代码结构

  1. application.js
  2. models/slideshow/slide.js
  3. collections/slideshow/slides.js
  4. views/slideshow.js

在 application.js 中我创建了我的路由器:

var App = {};
App.Modules = {
    Views: {},
    Models: {},
    Collections: {}
};
App.slideshow = undefined; // Use this to maintain state between calls.
App.router = (function() {
    var Router = Backbone.Router.extend({
        routes: {
            'slideshow/:id/:page': 'slideshow'
        },
        slideshow: function(id, page) {
            // Whenever this route handler triggers, I want to either:
            //     1) Instantiate the slideshow, or:
            //     2) Change the page on an already instantiated slideshow
            if (App.slideshow && App.slideshow.options.id === id) {
                App.slideshow.goToPage(page);
            } else {
                App.slideshow = new App.Modules.Views.Slideshow({
                    id: id,
                    page: page
                });
            }
        }
    });
    return new Router;
})();

// Using jQuery's document ready handler.
$(function() {
    Backbone.history.start({
        root: '/'
    });
});

这符合我的预期。我的幻灯片作为叠加层工作,因此无论在哪个页面上实例化它,它都会显示在现有文档的顶部。

我的第一个问题是如何关闭幻灯片(App.slideshow.close());当用户点击浏览器后退按钮或导航到另一个 hashbang 时,它不遵循 /slideshow/:id/:page 语法?

我的最后一个问题与路由器中的“导航”方法有关。在我的幻灯片视图中,我确保在页面更改时更新哈希片段。我认为这就是我所做的:

pageChange: function(page) {
    App.router.navigate('slideshow/' + this.options.id + '/' + page, false);
}

这可以确保片段得到更新,以便用户在任何时候都可以复制 URL 并且它将在同一页面上打开。问题是,即使我在第二个“导航”参数(triggerRoute)中传递 false,实例化路由器中的“幻灯片”方法也会触发。这是为什么呢?

I'm creating a slideshow using Backbone.js. My slideshow view is finished, each slide is a model and all the models are inside a collection. Now I want to apply a little hashbang magic to my slideshow :-)

This is my code structure

  1. application.js
  2. models/slideshow/slide.js
  3. collections/slideshow/slides.js
  4. views/slideshow.js

In application.js I create my router:

var App = {};
App.Modules = {
    Views: {},
    Models: {},
    Collections: {}
};
App.slideshow = undefined; // Use this to maintain state between calls.
App.router = (function() {
    var Router = Backbone.Router.extend({
        routes: {
            'slideshow/:id/:page': 'slideshow'
        },
        slideshow: function(id, page) {
            // Whenever this route handler triggers, I want to either:
            //     1) Instantiate the slideshow, or:
            //     2) Change the page on an already instantiated slideshow
            if (App.slideshow && App.slideshow.options.id === id) {
                App.slideshow.goToPage(page);
            } else {
                App.slideshow = new App.Modules.Views.Slideshow({
                    id: id,
                    page: page
                });
            }
        }
    });
    return new Router;
})();

// Using jQuery's document ready handler.
$(function() {
    Backbone.history.start({
        root: '/'
    });
});

This works as I expect. My slideshow works as an overlay so no matter what page it's instantiated on, it will just show itself on top of the existing document.

My first question is how do I close the slideshow (App.slideshow.close()); when the user hits the browser back button or navigates to another hashbang, which doesn't follow the /slideshow/:id/:page syntax?

My last question has to do with the 'navigate' method in Routers. In my slideshow view, I make sure to update the hash fragment whenever the page changes. This is what I do in my view:

pageChange: function(page) {
    App.router.navigate('slideshow/' + this.options.id + '/' + page, false);
}

This makes sure the fragment gets updated so that the user at any point can copy the URL and it will open on the same page. The problem is that my 'slideshow' method in my instantiated router triggers even though I pass false in the second 'navigate' parameter (triggerRoute). Why is this?

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

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

发布评论

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

评论(1

迟月 2024-12-22 20:23:53

所以,我想我已经弄清楚了。请告诉我是否有更干净的方法来做到这一点。

阅读http://msdn.microsoft.com/en-us/scriptjunkie/hh377172 我看到你可以在 Backbone.js 中执行此操作:

var router = Backbone.Router.extend({
    routes: {
        '*other': 'defaultRoute'
    },
    defaultRoute: function() {
        if (App.slideshow) App.slideshow.close();
    }
};

这可以确保与 /slideshow/:id/:page 不匹配的所有内容(如果已实例化)将关闭幻灯片。

关于“导航”显然是因为我做了 App.vent = _.extend({}, Backbone.events); 显然,我必须这样做:

App.vent = {};
_.extend(App.vent, Backbone.events);

So, I think I've figured it out. Please let me know if there are cleaner ways to do this.

After reading http://msdn.microsoft.com/en-us/scriptjunkie/hh377172 I saw you can do this in Backbone.js:

var router = Backbone.Router.extend({
    routes: {
        '*other': 'defaultRoute'
    },
    defaultRoute: function() {
        if (App.slideshow) App.slideshow.close();
    }
};

This makes sure everything that doesn't match /slideshow/:id/:page will close the slideshow if it's been instantiated.

With regard to 'navigate' apparently it's because I did App.vent = _.extend({}, Backbone.events); Apparently, I have to do:

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