2013-09-08 4 views
1

Скопированный пример с официального сайта emberjs под названием Mailbox (под ROUTING).Загрузите страницу загрузки Ember.js по адресу

App.Mailbox = Em.Object.extend(); 

App.Mailbox.reopenClass({ 
    find: function(id) { 
    if (id) { 
     return App.FIXTURES.findBy('id', id); 
    } else { 
     return App.FIXTURES; 
    } 
    } 
}); 

App.Router.map(function() { 
    this.resource('mailbox', { path: '/:mailbox_id' }, function() { 
    this.resource('mail', { path: '/:message_id' }); 
    }); 
}); 

App.ApplicationRoute = Em.Route.extend({ 
    model: function() { 
    return App.Mailbox.find(); 
    } 
}); 

App.MailRoute = Em.Route.extend({ 
    model: function(params) { 
    return this.modelFor('mailbox').messages.findBy('id', params.message_id); 
    } 
}); 

светильники:

App.FIXTURES = [ 
{ 
name: "Inbox", 
id: "inbox", 
messages: [ 
    { 
    id: 1, 
    subject: "Welcome to Ember", 
    from: "[email protected]", 
    to: "[email protected]", 
    date: new Date(), 
    body: "Welcome to Ember. We hope you enjoy your stay" 
    }, { 
    id: 2, 
    subject: "Great Ember Resources", 
    from: "[email protected]", 
    to: "[email protected]", 
    date: new Date(), 
    body: "Have you seen embercasts.com? How about emberaddons.com?" 
    }]}, { 
name: "Spam", 
id: "spam", 
messages: [ 
    { 
    id: 3, 
    subject: "You have one the Nigerian lottery!!!111ONEONE", 
    from: "[email protected]", 
    to: "[email protected]", 
    date: new Date(), 
    body: "You have ONE the lottery! You only have to send us a small amount of monies to claim your prize" 
    }]}, { 
name: "Sent Mail", 
id: "sent-mail", 
messages: [ 
    { 
    id: 4, 
    subject: "Should I use Ember", 
    from: "[email protected]", 
    to: "[email protected]", 
    date: new Date(), 
    body: "Ember looks pretty good, should I use it?" 
    }]}]; 

и HTML:

<script type="text/x-handlebars"> 
    <div class="url">URL: {{target.url}}</div> 
    <aside> 
     <ul> 
      <li><h2>Mailboxes</h2></li> 
     {{#each}} 
       <li> 
       {{#link-to "mailbox" this currentWhen="mailbox"}} 
         <span class="count"> 
         {{messages.length}} 
         </span> 
        {{name}} 
       {{/link-to}} 
       </li> 
     {{/each}} 
     </ul> 
    </aside> 

    <section class="main"> 
    {{outlet}} 
    </section> 
</script> 

<script type="text/x-handlebars" id="index"> 
<div class="index"> 
    <h1>TomsterMail</h1> 
    <span class="tomster"></span> 
</div> 
</script> 

<script type="text/x-handlebars" id="index"> 
<div class="mail"> 
    <dl> 
     <dt>From</dt> 
     <dd>{{from}}</dd> 
     <dt>To</dt> 
     <dd>{{to}}</dd> 
     <dt>Date</dt> 
     <dd>{{date date}}</dd> 
    </dl> 
    <h4>{{subject}}</h4> 
    <p>{{body}}</p> 
</div> 
</script> 
<script type="text/x-handlebars" id="mailbox"> 
<table> 
    <tr> 
     <th>Date</th> 
     <th>Subject</th> 
     <th>From</th> 
     <th>To</th> 
    </tr> 

{{#each messages}} 
    {{#link-to "mail" this tagName=tr}} 
      <td>{{date date}}</td> 
      <td>{{view.isActive}}{{subject}}</td> 
      <td>{{from}}</td> 
      <td>{{to}}</td> 
    {{/link-to}} 
{{/each}} 
</table> 

{{outlet}} 
</script> 

<script type="text/x-handlebars" id="mailbox/index"> 
<div class="mailbox-index"> 
Select an email 
</div> 
</script> 

Когда я иду по адр /index.html#/inbox/1 следующее исключение: брошенной

Assertion failed: Cannot call get with 'id' on an undefined object.

У меня есть веб-приложение, которое нуждается в такой же функциональности, но с запросами сервера (не из СВЕТИЛЬНИКИ)

ответ

0

Если я заменяю this.modelFor ('почтовый ящик'). get ('messages'). findBy ('id', params.message_id); с помощью AJAX вызова, то все работает, как ожидалось

App.MailRoute = Em.Route.extend({ 
    model: function(params) { 
    var ref = this; 
    return $.get("/inbox/"+params.message_id).then(function(data){ 
     return data.findBy('id', params.message_id); 
    }); 
    } 
}); 

});

0

Попробуйте изменить свой код для получения модели:

return this.modelFor('mailbox').get('messages').findBy('id', params.message_id); 

В Ember, вам нужно использовать получить() для извлекать свойства и ассоциации \

+0

Если это не сработает, укажите свои определения модели почтовых ящиков и сообщений – ianpetzer