灰烬选择默认值

时间:2015-06-03 17:51:10

标签: javascript ember.js ember-data

我无法在余烬选择中选择默认值。这是代码的样子

型号:

rfwa.Field = DS.Model.extend({
    Name: DS.attr('string'),
    FieldTypeId: DS.attr('number'),
    FieldType: DS.belongsTo('fieldType'),

});

rfwa.FieldType = DS.Model.extend({
    Name: DS.attr('string')
});

路线:

rfwa.FieldRoute = Ember.Route.extend({
   model: function (params) {
       var route = this;
       return Ember.RSVP.hash({
            field: route.store.find('field', params.field_id),
            fieldTypes: route.store.find('fieldType')
       });
    },
    setupController: function (controller, model) {
        controller.set('fieldTypes', model.fieldTypes);
        controller.set('model', model.field);
    }
});

模板:

<script type="text/x-handlebars" data-template-name="Field">
   {{input value=model.Name class="form-control" id="Name"}}
   {{view "select" content=fieldTypes optionValuePath="content.id" optionLabelPath="content.Name" value=model.FieldTypeId classNames="form-dropdown"}}
</script>

我在视图中使用选择和值来摆弄很多可能性。创建返回fieldType以供选择的函数。到目前为止,没有任何效果。我必须做错事。

我使用Ember 1.12.1和Ember-data 1.0.0-beta18。

目前使用我粘贴的内容,它会在页面加载时将我的model.FieldTypeId设置为undefined。

2 个答案:

答案 0 :(得分:1)

不确定您是否定义了控制器,但如果要在其上设置属性,则应至少定义一个具有这些属性的属性。我设置了一个jsbin来显示你可以做什么,只要设置默认选择并绑定它:

http://emberjs.jsbin.com/qijudidade/1/edit?html,js,output

答案 1 :(得分:0)

它之所以不起作用,是因为DS.Model中的id是一个字符串,而我的FieldTypeId是一个数字。我将模型改为

gremlin>  mongo = new GMongo()
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/gremlin-groovy-2.6.0/lib/logback-classic-0.9.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/gremlin-groovy-2.6.0/lib/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
22:03:28.590 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50}
22:03:28.652 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]
==>com.gmongo.GMongo@5286c33a
gremlin> 22:03:29.684 [cluster-ClusterId{value='5570af10acc16d0aec05e40e', description='null'}-127.0.0.1:27017] DEBUG org.mongodb.driver.connection - Closing connection connectionId{localValue:1}
22:03:29.699 [cluster-ClusterId{value='5570af10acc16d0aec05e40e', description='null'}-127.0.0.1:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongo-java-driver-3.0.2.jar:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongo-java-driver-3.0.2.jar:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) ~[mongo-java-driver-3.0.2.jar:na]
        at java.lang.Thread.run(Unknown Source) [na:1.8.0_45]
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_45]

这解决了我所有的问题。