设置属性值的灰烬问题

时间:2016-01-13 08:26:15

标签: javascript jquery ember.js

我的Ember Route类定义如下;

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8035/" />
            <add baseAddress="net.tcp://localhost:8032"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

我的模板看起来像

export default Ember.Route.extend({
    model: function() {
        var compObj = {};
        compObj.gridPara = this.get('gridPara');
        return compObj;
    },
    gridPara: function() {
        var self = this;
        var returnObj = {};
        returnObj.url = '/myService';
        // setting some other returnObj attributes
        var summaryObj = {
            total: {
                label: "Total 1",
                value: "100"
            },
            additional: [{
                label: 'Label 2',
                value: 'val2'
            }, {
                label: 'Label 3',
                value: 'val3'
            }]
        };
        returnObj.summary = summaryObj;
        return returnObj;
    },
    actions: {
        dataLoaded: function(resp) {
            // Here I get the service response and want to set (or overwrite) the summaryObj values
            this.get('gridParams').summary.total.value = resp.numRows;          
        }
    }
});

现在我想在returnObj上设置“summary” 我已经验证我在dataLoaded回调中得到“resp”。

但是在尝试

时出现以下错误
{{my-grid params=this.gridPara dataLoaded="dataLoaded"}}

未捕获错误:断言失败:您必须使用Ember.set()将this.get('gridParams').summary.total.value = resp.numRows; 属性([object Object])设置为value

另外如何在summaryObj

中设置/推送“附加”数组

3 个答案:

答案 0 :(得分:1)

如错误所述,您必须使用set来表示值(我假设您已在某处定义了gridParams?):

this.set('gridParams.summary.total.value', resp.numRows);

要推送新对象,请尝试以下操作:

var additional = this.get('gridParams.additional');
additional.push({label: ..., value: ....});
this.set('gridParams.additional', additional);

答案 1 :(得分:0)

不是100%肯定,但试一试:

  • 注意属性名称。我认为这是一个措辞错误,宣布'gridPara'并试图获得'gridParams'
  • 您应该像这样检索值 this.get('gridParams.summary.total.value')
  • 你在最后一句话中尝试的是一个设置,但它就像普通的JS一样。在Ember你应该做this.set('gridParams.summary.total.value',resp.numRows)

答案 2 :(得分:0)

只需添加@Remi答案,最佳做法是使用

Ember.set('gridParams.summary.total.value', resp.numRows);

回答评论中的问题 假设您要在索引additional更新i数组。只需执行

var updateItem = additional[i];
Ember.set(updateItem.propertyname,newValue)
//Here propertyname would be the property you want to update and new Value is the new value which you want to set to that property