我正在创建一个新的Ember.Model,其中包含来自ajax调用的返回响应。
这是我在控制器中使用的ajax方法
createImageResource: function() {
var self = this,
fd = new FormData(),
newResource = {},
resources = self.get('controllers.resources.model'),
fileName = this.get('resource_image').name,
fileSize = this.get('resource_image').size,
fileType = this.get('resource_image').type;
fd.append('resource[resource_type]', 'image');
fd.append('resource[resource_name]', this.get('resource_name'));
fd.append('resource[source_id]', this.get('source_id'));
fd.append('resource_image[resource_image]', this.get('resource_image'));
fd.append('resource[resource_file_name]', fileName);
fd.append('resource[resource_file_type]', fileType);
fd.append('resource[resource_file_size]', fileSize);
this.set('isProcessingResource', true);
$.ajax({
url: '/resources',
method: 'POST',
dataType: 'json',
processData: false,
contentType: false,
data: fd,
}).then(function(newResourceData) {
newResource = Msmapp.Resource.create(newResourceData.resource);
resources.pushObject(newResource);
self.set('isProcessingResource', false);
self.transitionToRoute('resource', newResource);
});
},这会将新资源添加到资源控制器使用的对象数组中。它像应该的那样把它放到DOM中。im所具有的问题是每个对象都是到各个资源的链接。所有存在于页面加载上的对象都可以正常工作。添加到列表中的对象具有正确的url和所有内容,当您尝试导航时,它不会执行任何操作。
我不确定在.then()中是否还有其他需要做的事情?
这是模板
<section class="column_list">
<ul>
{{#each resource in controller }}
<li class="item">
{{#if resource.isLoading }}
{{spinner}}
{{else}}
{{#linkTo 'resource' resource }}
<img {{bindAttr src='resource.listAvatar'}} />
<div class='title'>{{ resource.resource_name }}</div>
{{/linkTo}}
{{/if}}
</li>
{{/each}}
</ul>
</section>发布于 2013-07-01 05:24:01
既然您要添加到resources中,那么您也应该循环遍历{{#each resource in resources }}。
或者直接推送到您的ArrayController实例上- this. resources.pushObject(newResource);。
https://stackoverflow.com/questions/17357954
复制相似问题