Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - empty params variable in ember

So I've set up a router and some routes, and this works for the most part. when I load #/contacts/123 (or whatever) the ContactIndexRoute returns an empty params object. I'm sure this is relatively simple, but I cannot figure out why. Can someone point me in the right direction? Thanks.

CallMonitor.Router.map(function(){
    this.resource('contacts', function(){
        this.resource('contact', {path: '/:contact_id'}, function(){

        })
    });
});

CallMonitor.ContactsRoute = Ember.Route.extend({
    model: function(){
        return  this.store.find('contact');
    },
    setupController: function(controller, contacts) {
        var socket = CallMonitor.Socket;
        controller.set('socket', socket);
        controller.set('contact', contacts);
    },
    renderTemplate: function(){
        this._super(this, arguments);
        this.render('contacts', {
            into: 'application',
            outlet: 'contacts',
            controller: 'contacts'
        });
    }
});

CallMonitor.ContactIndexRoute = Ember.Route.extend({
    model: function(params){
        return  this.store.find('contact', params.contact_id);
    },
    renderTemplate: function(){
        this._super(this, arguments);
        this.render('contact', {
            outlet: 'points',
            into: 'contacts',
            controller: 'contactsIndex'
        })
    },
    setupController: function(controller, contact) {
        controller.set('contact', contact);
    }
});

CallMonitor.ContactsController = Ember.ArrayController.extend({
    actions: {
        getPoints: function(data){
            this.transitionToRoute('contact', data.id);
            console.log('the data is' + data );
        }
    },

    socketDidChange: function(){
        var socket = this.get('socket'),
            self = this;
        if(socket)
        {
            socket.on('call', function (data) {
                if(data.contactPointId){

                }
                else if (data.contactId)
                {
                    var contactToUpdate = self.contact.filter(function(item) {
                        return item.id == data.contactId;
                    });
                    if(contactToUpdate.length)
                    {
                        contactToUpdate[0].reload();
                    }
                    else
                    {
                        // reload all the contacts
                        var contactPromise = self.contact.store.find('contact');
                        contactPromise.then(function(data){
                            self.set('contact', data);
                        }, undefined);
                    }
                }
            });
        }
    }.observes('socket')
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Params are only passed to route which defines the slug. Meaning if you define a slug on the resource it only exists on the resource, and not its routes. If it's defined on a route under a resource it only lives on that route.

CallMonitor.ContactRoute = Ember.Route.extend({
    model: function(params){
        return  this.store.find('contact', params.contact_id);
    },
    renderTemplate: function(){
        this._super(this, arguments);
        this.render('contact', {
            outlet: 'points',
            into: 'contacts',
            controller: 'contactsIndex'
        })
    },
    setupController: function(controller, contact) {
        controller.set('contact', contact);
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...