在带有动态分段路由器v2的模型中使用ajax加载的最佳方法是什么 [英] What is the best way to use ajax loading for model with dynamic segments router v2

查看:78
本文介绍了在带有动态分段路由器v2的模型中使用ajax加载的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对动态细分路由器v2的问题。
是js的链接 http://jsfiddle.net/q9zvU/5/

I'm facing the problem with dynamic segments router v2. here is the link for js "http://jsfiddle.net/q9zvU/5/".

var App = Ember.Application.create();
App.deferReadiness();

App.ApplicationView = Ember.View.extend({
    templateName: 'campaign-app'
})
App.Router.map(function(match) {
    this.route('campaigns', {path: '/:type'})
})


App.CampaignsRoute = Ember.Route.extend({
    model: function() {
        alert('called model method');
        var data;
        $.ajax({
            url: '/echo/json/',
            async: false,
            data: {
                json: JSON.encode([{
                   name: 'Campaign 1',
                   type: 'scheduled'
                }, {
                   name: 'Campaign 2',
                   type: 'scheduled'
                }, {
                   name: 'Campaign 2',
                   type: 'draft'
                }])
            },
            dataType: 'json', 
            success: function(json) {
                alert(JSON.stringify(json));
                data = json;
            }
        })
        return data;
    },
    renderTemplate: function() {
        this.render({outlet: 'campaigns'});
    }
})
App.CampaignsView = Ember.CollectionView.extend({
    itemViewClass: Ember.View.extend({
        templateName: 'campaigns'
    })
})
App.advanceReadiness();

路线的模型方法不是通过linkTo和transitionTo调用的。 (在ember的文档中明确说明)。
但就我而言,我确实需要一个点来从路由中的ajax请求加载数据。

The model method of route isn't calling with linkTo and transitionTo. (It is clearly stated in ember's doc). But for my case, I really need to have a point to load the data from ajax request in route.

谢谢,
Linn

Thanks, Linn

推荐答案

Ember模型挂钩上的文档:

Ember Documentation on the model hook:


您可以实现一个钩子来将URL转换为此
路由的模型。

"A hook you can implement to convert the URL into the model for this route."

因此,模型挂钩只是在页面刷新时调用。但这使很多人感到困惑:-)。因此,在这种情况下,这不是正确的选择。我认为您应该在这种情况下使用setupController挂钩。试试这个:

So the model hook is just called on page refresh. But this confuses a lot of people :-). So, this is not the right hook in this case. I think you should use the setupController hook for this case. Try this:

App.CampaignsRoute = Ember.Route.extend({
    setupController: function(controller) {
        alert('called setupController method');
        var data;
        $.ajax({
            url: '/echo/json/',
            async: false,
            data: {
                json: JSON.encode([{
                   name: 'Campaign 1',
                   type: 'scheduled'
                }, {
                   name: 'Campaign 2',
                   type: 'scheduled'
                }, {
                   name: 'Campaign 2',
                   type: 'draft'
                }])
            },
            dataType: 'json', 
            success: function(json) {
                alert(JSON.stringify(json));
                data = json;
            }
        });
        controller.set("model",data);
    }
});

一些额外的说明:您似乎没有包裹您的包裹很尴尬Ember.Object中的对象。通常,您不使用普通的JS对象。

A little extra remark: It seems awkward that you do not wrap your objects in an Ember.Object. Normally you do not work on ordinary JS objects.

这篇关于在带有动态分段路由器v2的模型中使用ajax加载的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆