jquery移动骨干路由

时间:2013-05-07 11:20:37

标签: jquery jquery-mobile backbone.js requirejs underscore.js

我正在使用jquery mobile和backbonejs。 有点坚持使用支持转换属性的主干路由。 任何建议都会很棒..

      define(['jquery'], function ($) {
      $(document).on("mobileinit", function () {
          $.mobile.ajaxEnabled = false;
          $.mobile.linkBindingEnabled = false;
          $.mobile.hashListeningEnabled = false;
          $.mobile.pushStateEnabled = false;
          $.mobile.defaultPageTransition = "slide";
     });
});

但幻灯片过渡无效。


路由器的代码在

之下
define([
'jquery',
'underscore',
'backbone',
'backbone.subroute'], function($, _, Backbone) {
var AppRouter = Backbone.Router.extend({
    routes: {
        // general routes 
        '': 'defaultAction',
        'login':'login',
        'message':'message',
        'menu': 'mainMenu',

        // Default
        '*actions': 'defaultAction'
    }
});

var initialize = function() {

        $('.back').live('click', function(event) {
            event.preventDefault();
            window.history.back();
            return false;
        });

    var app_router = new AppRouter;
    app_router.on('route:defaultAction', function(actions) {
        require(['views/home/register'], function(RegisterView) {
            // We have no matching route, lets display the home page 
            console.log('At defaultAction');
        var registerView = new RegisterView();
        registerView.render();
           /// this.changePage(loginView, 'slide');
        });
    });

     app_router.on('route:login', function(actions) {
        require(['views/home/login'], function(LoginView) {
            // We have no matching route, lets display the home page 
            console.log('At defaultAction');
        var loginView = new LoginView();
        loginView.render();
           /// this.changePage(loginView, 'slide');
        });
    });
    app_router.on('route:mainMenu', function(actions) {
        require(['views/home/menu'], function(MainMenuView) {
            console.log('At mainMenu::router');
        var mainMenuView = new MainMenuView();
        mainMenuView.render();
          //  this.changePage(mainMenuView, 'slide');
});
    });

我们可以在这里使用cal changePage进行过渡吗?

1 个答案:

答案 0 :(得分:1)

这可以在init.js文件中。

(函数($){

    var appRouter = Backbone.Router.extend({ 
            routes: {   "": "showActivitiesPage"
            },

            showActivitiesPage: function() {                
                $.mobile.changePage("#activities", { reverse: false, changeHash: false });                          
            }           
    }); 

}(jQuery));

即使哈希监听设置为false,您也可以使用更改页面JQM / backbone不会阻止我们使用更改页面和哈希路由。

查看此链接,它在所有场景中都有一个示例,但您可以将其分解为控制器,模型和视图

http://blog.chariotsolutions.com/2011/12/introduction-to-backbonejs-with-jquery.html

在上述例子的上下文中:

控制器:

(function($){
$('#activities').live('pageinit', function(event){
    var activitiesListContainer = $('#activities').find(":jqmData(role='content')"),
        activitiesListView;
    exercise.initData();
    activitiesListView = new exercise.ActivityListView({collection: exercise.activities, viewContainer: activitiesListContainer});
    activitiesListView.render();
});

}(jQuery));

型号:

(函数($){

exercise.Activity = Backbone.Model.extend({
});

exercise.Activities = Backbone.Collection.extend({
    model: exercise.Activity,
    url: "exercise.json"
});

}(jQuery的));

查看:

(function($){
exercise.ActivityListView = Backbone.View.extend({
        tagName: 'ul',
        id: 'activities-list',
        attributes: {"data-role": 'listview'},

        initialize: function() {
            this.template = _.template($('#activity-list-item-template').html());
        },

        render: function() {
            var container = this.options.viewContainer,
                activities = this.collection,
                template = this.template,
                listView = $(this.el);

            $(this.el).empty();
            activities.each(function(activity){
                listView.append(template(activity.toJSON()));
            });
            container.html($(this.el));
            container.trigger('create');
            return this;
        }
    });
}(jQuery));

运动可以是全局变量。