简体   繁体   English

具有视图的Emberjs路由模型

[英]Emberjs route model with view

My app has a page where I'm using the view to display the data from other template with my view like this : 我的应用程序有一个页面,在该页面上,我使用视图来显示其他模板中的数据,如下所示:

<script type="text/x-handlebars" data-template-name="enquiry">
        [...] // some other information display before
    {{view App.EnquirySelectedVehicleView}}
</script>

<script type="text/x-handlebars" data-template-name="selectedVehicle">
    // Here is my content
</script>

My map looks like this : 我的地图看起来像这样:

this.resource('enquiry', { path: '/enquiry/:enquiry_id'}, function() {
    this.route('selectedVehicle');
});

After reading the doc I just did this in my view : 在阅读了文档之后,我认为这样做是:

App.EnquirySelectedVehicleView = Ember.View.extend({
    templateName: 'selectedVehicle'
});

So far so good, its showing the text from my template. 到目前为止一切顺利,它显示了我模板中的文本。 But I need to return data from an ajax call in this template (selectedVehicle) automatically, like its fetching the data when you are on /enquiry/1/. 但是我需要自动从此模板(selectedVehicle)中的ajax调用返回数据,就像在/ enquiry / 1 /上获取数据一样。

I've done this in my router : 我已经在路由器中完成了此操作:

App.EnquirySelectedVehicle = Ember.Object.extend({});

App.EnquirySelectedVehicleRoute = Ember.Route.extend({
    model: function() {
        console.log('DEBUG: SelectedVehicle Model');
        App.SelectedVehicle.vehicleStock(this)
    }
});

App.EnquirySelectedVehicle.reopenClass({
    vehicleStock: function(that) {
        console.log('DEBUG: Fetch vehicle stock');
        // Here come the ajax call
    }
});

But my issue is that route is never call.. How can I return some value from my selectedVehicleRoute when I'm on the /enquiry/1 page in a view template ? 但是我的问题是,路由永远都不会被调用。当我位于视图模板的/ enquiry / 1页面上时,如何从selectedVehicleRoute返回一些值? (not sure if I ask it correctly) (不确定我是否正确询问)

Thanks for the help ! 谢谢您的帮助 !

[edit] [编辑]

@Fanta : I think I begin to understand how I can do that : @Fanta:我想我开始理解我该怎么做:

App.EnquiryRoute = Ember.Route.extend({
    beforeModel: function(transition) {
        this.controllerFor('login').send('isSession', transition);
    },
    model: function(param) {
        var promise = new Ember.RSVP.Promise(function(resolve, reject) {
            var modelData = {enquiry: {}, vehicleStock: {}};
            Ember.$
            .get(host + '/enquiry/' + param['enquiry_id'], function(data) {
                console.log('DEBUG: Enquriry GET OK id = ' + param['enquiry_id']);
                modelData.enquiry = data.enquiry;
                Ember.$.get(host + '/vehiclestock/' + data.enquiry.VehicleStockId, function(data) {
                    console.log('DEBUG: VehicleStock GET OK id = ' + data.enquiry.VehicleStockId)
                    console.log(data);
                    modelData.vehicleStock = data.vehicleStock;
                    resolve(modelData);
                });
            });
        });
        return promise;
    }
});

It seems to work, now I have to figure it out how to display my Object :) but thank you for your help, that actually make me resolve it by a different way ! 它似乎可行,现在我必须弄清楚如何显示我的Object :),但感谢您的帮助,实际上使我可以通过其他方式解决它!

For future reference, just go to https://github.com/emberjs/ember.js/blob/master/CONTRIBUTING.md and you'll see two links, one to JSFiddle and one to a JSBin with the basic setup. 为了将来参考,只需访问https://github.com/emberjs/ember.js/blob/master/CONTRIBUTING.md ,您将看到两个链接,一个链接到JSFiddle,一个链接到具有基本设置的JSBin。

Are you sure the route is not being called ? 您确定未呼叫该路线吗? I created a Fiddle, http://jsfiddle.net/NQKvy/817/ if you check the JS console, you'll see in the log: 我创建了一个Fiddle, http://jsfiddle.net/NQKvy/817/,如果您检查JS控制台,则会在日志中看到:

DEBUG: SelectedVehicle Model
DEBUG: Fetch vehicle stock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM