2016-09-28 10 views
-1

マイアプリinitが見え、私はすべてを試してみましたBackbone.history.navigateに動作していない

var $ = jQuery = require('jquery'), 
    Backbone = require('backbone'), 
    loginView = require('./views/login/LoginView'), 
    userActivitiesMainView = require('./views/dashboard/UserActivitiesMainView'), 
    mainDashboardView = require('./views/dashboard/MainDashboardView'); 

var Router = Backbone.Router.extend({ 
    initialize: function (options) { 
     this.options = options; 
     this.currentView = null; 
    }, 
    setCurrentView: function (view) { 
     if (this.currentView && 
      typeof this.currentView.onClose != 'undefined') { 
      this.currentView.onClose(); 
     } 

     this.currentView = view; 
     $('body').scrollTop(0); 

    }, 
    routes: { 
     "": "home", 
     "login(/)": "home", 
     "dashboard(/)": "dashboard", 
    }, 
    notFound: function() { 
     this.setCurrentView(null); 
     $('.div-main').html('<h2>Oops! Page not found. Please try going back from the browser.</h2>'); 
    }, 
    dashboard: function() { 
     "use strict"; 
     var dashboard = new mainDashboardView(); 
     this.setCurrentView(dashboard); 
     this.addSideBar(); 
     this.addHeaderBar(); 
    }, 
    home: function() { 
     "use strict"; 
     var login = new loginView(); 
     this.setCurrentView(login); 
    }, 
    addSideBar: function() { 
     var sidebar = new sidebarView(); 
     //this.setCurrentView(sidebar); 
    }, 
    //Function to add header 
    addHeaderBar: function() { 
     var header = new headerView(); 
     //this.setCurrentView(header); 
    } 

}) 
module.exports = Router; 


    var ret = Backbone.history.navigate('dashboard', true); 
     console.log(ret); // comes false for me 

それは動作しませんが

編集:

私はビューで次のようにナビゲートしています:

events: function() { 
    return this.appendEvents({ 
     'click #btnContinue': 'login' 
    }) 
}, 
login: function() { 
    var otp = this.$el.find('#otp').val(); 
    var data = { 
     "mobile_number": sessionStorage.getItem('mobile_number'), 
     "otp": otp, 
     "name_of_user": "Vini", 
     "flag": "payment_link", 
     "title": "Pen and Pencil and rose", 
     "description": "You can write and erase and smile and pay", 
     "amount": "50.00", 
     "images": [ 
      "ac19fa94-9f5f-48e0-93a7-53d5da616ac3", 
      "13da680e-6050-40e0-97c8-cc141d1439d9" 
     ] 
    }; 
    this.loginModel.set(data); 
    this.loginModel.save().then(function (response) { 
     console.log(response); 
     Cookies.set('API', response); 
     var ret = Backbone.history.navigate('#dashboard', { trigger: true}); 
     console.log(ret); 
    }) 
} 
+0

予想される動作は何ですか?代わりに本当に何が起こっていますか?正確な問題は何ですか? –

答えて

0

問題がBackbone.history.start()を呼び出す前navigateを呼び出してから来ています。

ルータを定義した直後にBackbone.history.navigate('dashboard', true)に電話をかけますが、readyBackbone.history.start()と電話してください。あなたは、リンクのクリックで移動したい場合は

$(document).ready(function() { 

     this.router = new Router(); 

     Backbone.history.start(); 

     // Initial navigate can be done here 
     Backbone.history.navigate('dashboard', true); 
     // or 
     this.router.navigate('dashboard', true); 
    }); 

また、あなたが他のリンクタグとしてhrefプロパティを使用することができます。

<a href="#/dashboard">Dashboard</a> 

あなたは次のようなフラグメントのルートを使用している場合は、あなたがしたいルートの前で​​でnavigateを呼び出すことを忘れないでください。

http://example.com/page/#/fragment/route/here 
+0

私はdocument.ready関数で何をしたいのですか? – vini

+0

@vini問題は明らかにあなたの質問では定義されていません。ちょうど大きなコードスニペットです...私はあなたの 'Router'の最後の行を参照していました。 –

+0

確かにそのことを申し訳ありません。 – vini

関連する問題