2

我有以下代码。(我只是复制路由器,因为该应用程序目前大约有 500 行)我需要的是从状态“房间”切换到“guests.new”。

当我进入房间状态时,它会加载一个模板,我在其中调用操作“newGuest”以将状态更改为“newGuest”屏幕。但我无法让它工作。在萤火虫我得到这个错误:

g[a] is undefined

这是路由器代码。希望您能够帮助我。

App.Router = Ember.Router.extend({
        enableLogging:true,
        location:'hash',
        gotoRooms:Ember.Route.transitionTo('rooms.index'),
        gotoGuests:Ember.Route.transitionTo('guests.index'),
        gotoBookings:Ember.Route.transitionTo('bookings'),
        gotoHome:Ember.Route.transitionTo('root.index'),

        root:Ember.Route.extend({
            index:Ember.Route.extend({
                route:'/',
                connectOutlets:function (router) {
                    App.HomeView.appendTo('#main-content .container_12');
                },
                enter: function(){
                    console.log('Entro Home');
                },
                exit: function(){
                    App.HomeView.remove();
                }
            }),
            guests:Ember.Route.extend({
                route:'/guests',
                index:Ember.Route.extend({
                    route:'/',
                    connectOutlets:function(router) {
                        App.GuestsView.appendTo('#main-content .container_12');    
                    },
                    enter: function(){
                        console.log("Entro Guests");
                    },
                    exit: function(){
                        App.GuestsView.remove();
                    },
                }),
                new:Ember.Route.extend({
                    route:'/new/:bed',
                    deserialize: function(manager, params) {
                        console.log('New Guest \nBed:'+params['bed']+"\n Date:"+params['date']);
                        App.GuestView.appendTo('#main-content .container_12');
                    },
                    connectOutlets:function(){
                        alert("hola");
                    },
                    exit: function(){
                        App.GuestsView.newGuestView.remove();
                    }
                })
            }),
            rooms:Ember.Route.extend({
                newGuest:Ember.Route.transitionTo('guests.new'),
                route:'/rooms',
                index:Ember.Route.extend({
                    route:'/',                    
                    connectOutlets:function(router) {
                        App.RoomsView.appendTo('#main-content .container_12');    
                    },
                    enter: function(){
                        console.log("Entro Rooms");
                    },
                    exit: function(){
                        App.RoomsView.remove();
                    }
                })
            })
        })
    });

谢谢@Akash,但它仍然无法正常工作。我正在使用 ember-1.0.pre。调用“newGuest”的模板如下:

  <script type="text/x-handlebars" data-template-name="rooms">
      {{#each room in App.Rooms}}
        <div class="grid_4">
      <div class="block-border">
        <div class="block-header">
          <h1>{{room.screen}}</h1><span></span>
        </div>
        <div class="block-content">
          <ul class="block-list">
          {{#each bed in room.camas}}
            {{#if bed.guestId}}
            <li {{action "viewGuest" on="click"}} class="occupied">{{bed.id_guest}}</li>
            {{else}}
            <li class="free" {{action "newGuest" on="click"}}>Free Bed</li>
            {{/if}}
          {{/each}}
          </ul>
        </div>
      </div>
    </div>
      {{/each}}
    </script>

我也尝试过使用 ember-latest,因为我在各种线程中读到了预版本给出了一些路由问题。现在事实证明,使用 ember-latest,我无法定义计算属性。至少这是加载页面时控制台上弹出的内容。

有什么线索吗?

非常感谢!

4

1 回答 1

1

问题是 Ember 无法guests从路线内访问rooms路线。所以你需要做的就是将动作函数移到树的下方。我在下面稍微调整了您的代码:

调整代码:

App.Router = Ember.Router.extend({
        enableLogging:true,
        location:'hash',
        gotoRooms:Ember.Route.transitionTo('rooms.index'),
        gotoGuests:Ember.Route.transitionTo('guests.index'),
        gotoBookings:Ember.Route.transitionTo('bookings'),
        gotoHome:Ember.Route.transitionTo('root.index'),

        root:Ember.Route.extend({
            newGuest:Ember.Route.transitionTo('guests.new'),
            index:Ember.Route.extend({
                route:'/',
                connectOutlets:function (router) {
                    App.HomeView.appendTo('#main-content .container_12');
                },
                enter: function(){
                    console.log('Entro Home');
                },
                exit: function(){
                    App.HomeView.remove();
                }
            }),
            guests:Ember.Route.extend({
                route:'/guests',
                index:Ember.Route.extend({
                    route:'/',
                    connectOutlets:function(router) {
                        App.GuestsView.appendTo('#main-content .container_12');    
                    },
                    enter: function(){
                        console.log("Entro Guests");
                    },
                    exit: function(){
                        App.GuestsView.remove();
                    },
                }),
                new:Ember.Route.extend({
                    route:'/new/:bed',
                    deserialize: function(manager, params) {
                        console.log('New Guest \nBed:'+params['bed']+"\n Date:"+params['date']);
                        App.GuestView.appendTo('#main-content .container_12');
                    },
                    connectOutlets:function(){
                        alert("hola");
                    },
                    exit: function(){
                        App.GuestsView.newGuestView.remove();
                    }
                })
            }),
            rooms:Ember.Route.extend({
                route:'/rooms',
                index:Ember.Route.extend({
                    route:'/',                    
                    connectOutlets:function(router) {
                        App.RoomsView.appendTo('#main-content .container_12');    
                    },
                    enter: function(){
                        console.log("Entro Rooms");
                    },
                    exit: function(){
                        App.RoomsView.remove();
                    }
                })
            })
        })
    });
于 2012-12-29T08:37:43.300 回答