Jquery Mobile在重定向或更改网址时,页面没有任何CSS

时间:2013-08-24 06:52:37

标签: html node.js jquery-mobile backbone.js requirejs

我正在使用骨干,jquery mobile,express app。当应用程序启动并正常工作时,一切看起来都很好,但是,当我单击链接或更改URL时,html正确呈现但没有出现jquery移动魔术。它只在登录部分中呈现页眉和页脚和格式,但是当URL更改并且我回来时,页面将丢失其css或jquery移动魔术。

define(['views/index', 'views/register', 'views/login', 'views/forgotpassword', 'views/profile',
     'views/vinbookDoc', 'models/Account', 'models/Vinbook', 'models/vinBooksCollection'],

  function(IndexView, RegisterView, LoginView, ForgotPasswordView, ProfileView, 
                        vinbookDocView,  Account, Vinbook, vinBooksCollection) {

  var AppRouter = Backbone.Router.extend({
    currentView: null,

    routes: {
      "index": "index",
      "login": "login",
      "desk/:id": "desk",
      "profile/:id": "profile",
      "register": "register",
      "forgotpassword": "forgotpassword",
      "vinbook/:id": "showVinbook"
    },

    initialize: function(){
       $('.back').live('click', function(event) {
            window.history.back();
            return false;
        });
        this.firstPage = true;
    },

    showVinbook: function(id) {

        var getCollection = new vinBooksCollection();
        getCollection.url = '/accounts/me/vinbook';
        this.changeView( new vinbookDocView({ 
            collection: getCollection,
            id: id
        }));
        getCollection.fetch();
    },

    changeView: function(page) {

      this.currentView = page; 
      $(this.currentView.el).attr('data-role', 'page');
        this.currentView.render();
        $('body').append($(this.currentView.el));
        var transition = $.mobile.defaultPageTransition;
        // We don't want to slide the first page
        if (this.firstPage) {
            transition = 'none';
            this.firstPage = false;
        }
        $.mobile.changePage($(this.currentView.el), {changeHash:false, transition: transition});

    },

    index: function() {
      this.changeView(new IndexView() );
    },

    desk: function (id){
      var model = new Account({id:id});
      this.changeView(new ProfileView({model:model}));
      model.fetch({ error: function(response){  console.log ('error'+JSON.stringify(response));  } });
      console.log('works');
    }, 

    profile: function (id){
      this.changeView(new IndexView() );
    }, 

    login: function() {
      this.changeView(new LoginView());
    },

    forgotpassword: function() {
      this.changeView(new ForgotPasswordView());
    },

    register: function() {
      this.changeView(new RegisterView());
    }
  });

  return new AppRouter();
});

要求

require.config({
  paths: {
    jQuery: '/js/libs/jquery',
    jQueryUIL: '/js/libs/jqueryUI', 
    jQueryMobile: '/js/libs/jqueryMobile', 
    Underscore: '/js/libs/underscore',
    Backbone: '/js/libs/backbone',
    models: 'models',
    text: '/js/libs/text',
    templates: '../templates',
    jqm: '/js/jqm-config',

    AppView: '/js/AppView'
  },

  shim: {
    'jQueryMobile': ['jQuery', 'jqm' ],
    'jQueryUIL': ['jQuery'], 

    'Backbone': ['Underscore', 'jQuery', 'jQueryMobile',  'jQueryUIL'],
    'AppView': ['Backbone']
  }
});

require(['AppView' ], function(AppView) {
  AppView.initialize();
});

登录

define(['AppView','text!templates/login.html'], function(AppView, loginTemplate) {
  window.loginView = AppView.extend({
    requireLogin: false,

    el: $('#content'),

    events: {
      "submit form": "login"
    },

    initialize: function (){
        $.get('/login', {}, function(data){}); 
    },  

    login: function() {

      $.post('/login', {
        email: $('input[name=email]').val(),
        password: $('input[name=password]').val()
      }, 

      function(data) {
        console.log(data);
        if (!data.error){window.location.replace('#desk/me');}

      }).error(function(){
        $("#error").text('Unable to login.');
        $("#error").slideDown();
      });

      return false;
    },

    render: function() {
      this.$el.html(loginTemplate);
      $("#error").hide();
      return this;
    }
  });

  return loginView;
});

更多细节:

当我从网页或网址更改为其他网页时,会显示呈现网站的闪光,然后css或设计消失。

1 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题:

$(document).bind('pagechange', function() {
  $('.ui-page-active .ui-listview').listview('refresh');
  $('.ui-page-active :jqmData(role=content)').trigger('create');
});