Backbone + JQuery Mobile + RequireJS

时间:2014-01-06 09:04:13

标签: jquery-mobile backbone.js requirejs

我遇到了RequireJS的大问题。此配置随机工作。我不知道为什么,一旦它起作用,一旦它不起作用:

requirejs.config({
  baseUrl: 'js',
  urlArgs: "bust=" + (new Date()).getTime(),
  paths: {
    jquery: 'libs/jquery/jquery-1.10.2.min',
    underscore: 'libs/underscore/underscore-min',
    backbone: 'libs/backbone/backbone-min',
    marionette: 'libs/marionette/backbone.marionette',
    cordova: 'libs/cordova/cordova-1.9.0',
    jquerym: 'libs/jquery-mobile/jquery.mobile-1.4.0'
  },

  shim: {
    'jquery': {
      deps: []
    },
    'jquerym': {
      deps: ['jquery'],
      exports: 'jquery'
    },
    'underscore': {
      deps: [],
      exports: "_"
    },
    'backbone': {
      deps: ['jquery', 'underscore'],
      exports: 'Backbone'
    },
    'marionette': {
      deps: ['jquery', 'underscore', 'backbone']
    }
  },
  priority: ['jquery', 'jquerym']
});


require(['app', 'jquery', 'jquerym'], function (App) {
  $(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
    $.mobile.linkBindingEnabled = false;
    $.mobile.hashListeningEnabled = false;
    $.mobile.pushStateEnabled = false;
    // Remove page from DOM when it's being replaced 
    $('div[data-role="page"]').live('pagehide', function (event, ui) {
      $(event.currentTarget).remove();
    });
  });

  console.log('jQuery version ' + $().jquery + ' installed');
  App.initialize();
});

1 个答案:

答案 0 :(得分:3)

在加载jQuery Mobile之前必须绑定Mobile-Init-Event才能正确触发它。 尝试使用这样的东西:

require(["jquery"], function( $ ){
    $( document ).one( "mobileinit", function() {
        //Set your configuration and event binding
        $.mobile.ajaxEnabled = false;
        $.mobile.linkBindingEnabled = false;
        $.mobile.hashListeningEnabled = false;
        $.mobile.pushStateEnabled = false;
        // Remove page from DOM when it's being replaced 
        $('div[data-role="page"]').live('pagehide', function (event, ui) {
            $(event.currentTarget).remove();
        });
    });

    require( [ "App", "jquerym" ], function( App, jqm ) {
        /* Do Stuff with jqm here if you want like jqm.initializePage() if turned off */
        App.initialize();
    });
});