2013-10-25 50 views
0

我正在Ember.js中构建一个应用程序,并且遇到监视我向用户显示的属性的问题。当用户登录时,我正在查询一个API,以获得具有名为“isTrue”的字段的消息数量并查看有多少个值为“true”。Ember.js控制器属性问题

当用户登录到系统中我从他们的Cookie在应用控制器通过在init抓一个ID:

该数据然后被显示在应用程序模板函数()。这是做这件事的最好方法吗?我有一个问题,当用户单击按钮将记录从“isNew”==“true”更改为“isNew”==“false”时,该属性未正确更新。这是做这件事的最好方法,还是我从Ember的角度来看它是错误的?

App.ApplicationController = Ember.Controller.extend({ 

    messageCount: null, 

    init: function() { 

     var test = 0; 

     //get the cookie 
     //query an API to retrieve a set of records to check if a specific field is marked as true 
     //Compute the messageCount based on the number of records that are marked as true and set test equal to the message count. 

     this.set("messageCount", test); 

    getMessages: function() { 

     //repeat the code from the init function 
    } 

    actions: { 

     markAsRead: function() { 
      getMessages(); 
     } 
    } 

}) 
+0

'isNew'在哪里玩?当你点击行动'markAsRead'?财产(我猜'messageCount')如何得到更新,只有在客户端或服务器上?我想我只是没有看到你试图更改记录的位置...... – Hana

+0

@Hana'isNew'存储在服务器上,每次用户登录时都会通过API查询或者他们将消息标记为已读通过传播到markAsRead函数的操作。 – datapanda

回答

0

检查他们如何跟踪此简单To Do List example中已完成任务的数量。

你应该设置你属性是这样的:

messageCount: function() { 
    return this.filterProperty('isNew', true).get('length'); 
}.property('[email protected]'), 

无需现在手动调整时,它会自动更新。

+0

如果将属性值存储在服务器上时,你会如何加入?每次用户单击markAsRead时,通过API调用将该记录的属性“isNew”更改为false,然后重新读取记录,并相应地更新属性messageCount。 – datapanda

相关问题