本文介绍了路由仅在页面刷新时加载 - Ember JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Ember,我正在制作一个简单的应用程序,但是我遇到了一个奇怪的问题。我有路由设置,只有当我重新加载页面时才拉数据。这是我的代码:

  //启动Ember应用程序
App = Ember.Application.create({
LOG_TRANSITIONS:true
});

//路由器路径
App.Router.map(function(){

//首页
this.resource('index',{路径:'/'});

//书查看
this.resource('book',{path:'/ book /:id /:version'});

});

//主页路由
App.IndexRoute = Ember.Route.extend({
model:function(){
//获取所有的书
返回Ember。$。getJSON('/ books');
}
});


//单本图书
App.BookRoute = Ember.Route.extend({
model:function(params){
// Get一本书
返回Ember。$。getJSON('/ book?id ='+ params.id +'& version ='+ params.version);
}
});

当我从主页点击/#/ book / 1/1时,页面是空白。当我刚刚刷新页面时,我会加载数据,一切正常。



为什么是这样?

解决方案

感谢大家的建议,我可以做些什么来让用户从主页点击?我在这里找到了这个代码:

  App.BookRoute = Ember.Route.extend({
setupController:函数(控制器,模型){
//获取一本书
Ember。$。getJSON('/ book?id ='+ model.id +'& version ='+ model.version,
函数(数据){
controller.set('model',data);
});
}
});

我用setupController代替模型。


I am currently learning Ember and I am making a simple app, but I have encountered a strange problem. I have a route setup and it only pulls the data when I reload the page. Here is my code:

// Start Ember application
App = Ember.Application.create({
    LOG_TRANSITIONS: true
});

// Router paths
App.Router.map(function () {

    // Homepage
    this.resource('index', { path: '/' });

    // Book view
    this.resource('book', { path: '/book/:id/:version' });

});

// Homepage route
App.IndexRoute = Ember.Route.extend({
    model: function () {
        // Get all the books
        return Ember.$.getJSON('/books');
    }
});


// Single book view
App.BookRoute = Ember.Route.extend({
    model: function (params) {
        // Get a single book
        return Ember.$.getJSON('/book?id=' + params.id + '&version=' + params.version);
    }
});

When I go to /#/book/1/1 by clicking from the homepage, the page is blank. When I just refresh the page when I'm on there it loads the data and everything works.

Why is this? What can I do to make it work when the user clicks from the homepage?

解决方案

Thank you everyone for your suggestions. I figured it out with this code here:

App.BookRoute = Ember.Route.extend({
    setupController: function (controller,model) {
        // Get a single book
        Ember.$.getJSON('/book?id=' + model.id + '&version=' + model.version,
            function (data) {
                controller.set('model',data);
            });
    }
});

I used setupController instead of model.

这篇关于路由仅在页面刷新时加载 - Ember JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:50