首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用ember-model的deleteRecord()

无法使用ember-model的deleteRecord()
EN

Stack Overflow用户
提问于 2013-09-08 22:18:20
回答 1查看 676关注 0票数 0

在这里,我正尝试在单击行删除选项(十字)时从表中删除行。但是它抛出了一个错误Uncaught TypeError: Object 2 has no method 'call'

在这里,我按如下方式显示模型中的数据:

代码语言:javascript
复制
<tbody>                           
    {{#each item in model}}
        {{#each item in item.cart_items}}
        <tr>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.qty}}</td>
            <td>{{item.price}}</td>
            <td>{{item.subtotal}}</td>
            <td><button type="button" {{action 'deleteproduct' item.id}} class="close" aria-hidden="true" >&times;</button></td>
        </tr>   
        {{/each}}
    {{/each}}                                                             
</tbody>

上述夹具的结构如下:

代码语言:javascript
复制
Astcart.Application.adapter = Ember.FixtureAdapter.create();

Astcart.Application.FIXTURES = [
    {
        "logged_in": {
            "logged": true,
            "username": "sachin",
            "account_id": "4214"
        },
        "cart_items": [
            {
                "id": "1",
                "name": "Samsung Galaxy Tab 2",
                "qty": "1",
                "price": "1245.12",
                "subtotal": "1245.12"
            },
            {
                "id": "2",
                "name": "Samsung Galaxy Tab 2",
                "qty": "2",
                "price": "1500.00",
                "subtotal": "3000.00"
            },
            {
                "id": "3",
                "name": "Samsung Galaxy Tab 2",
                "qty": "5",
                "price": "100.00",
                "subtotal": "500.00"
            }
        ]           
    }
];

在这里,我尝试从表中删除行:

代码语言:javascript
复制
deleteproduct: function(productID){
    if (window.confirm("Are you sure you want to delete this record?")) {           
        var result = this.get('model').map(function(application) {
            console.log(JSON.stringify(application.get('cart_items')));
            console.log(JSON.stringify(application.get('cart_items').find(productID)));
            application.get('cart_items').deleteRecord(application.get('cart_items').find(productID));
            application.get('cart_items').commit();
        });
    }
}

I have posted my complete code here.有人能帮我把这把小提琴弄好吗?

更新

I have updated my code here deleteRecord在上面的问题中工作得很好,但是如何从一个数组中删除记录,该数组是fixture的一部分(节点之一)。

代码语言:javascript
复制
cart_items is an array and node of fixture as explained above.

I have posted my complete code here.有人能帮我把这把小提琴弄好吗?

EN

回答 1

Stack Overflow用户

发布于 2013-09-09 01:16:02

您可以通过操作直接发送item

代码语言:javascript
复制
<td><button type="button" {{action 'deleteproduct' item}} class="close" aria-hidden="true" >&times;</button></td>

然后将其删除

代码语言:javascript
复制
Astcart.IndexController = Ember.ArrayController.extend({
    deleteproduct: function(product){
        if (window.confirm("Are you sure you want to delete this record?")) {               
            product.deleteRecord();
        }
    }
}); 

不要忘记在模型上运行fixture适配器:

代码语言:javascript
复制
Astcart.Cart_items.adapter = Ember.FixtureAdapter.create();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18684744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档