0

在这里,我试图通过单击行删除选项(十字符号)从表中删除行。但它抛出一个错误Uncaught TypeError: Object 2 has no method 'call'

在这里,我显示来自模型的数据如下:

<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>

上述夹具结构如下:

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"
            }
        ]           
    }
];

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

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();
        });
    }
}

我在这里发布了我的完整代码。 任何人都可以帮我制作这个小提琴吗?

更新

我在这里更新了我的代码 deleteRecord 在上面的小提琴中工作正常,但是如何从作为夹具一部分(节点之一)的数组中删除记录。

cart_items is an array and node of fixture as explained above.

我在这里发布了我的完整代码。 任何人都可以帮我制作这个小提琴吗?

4

1 回答 1

2

您可以item通过操作发送

<td><button type="button" {{action 'deleteproduct' item}} class="close" aria-hidden="true" >&times;</button></td>

然后删除它

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

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

Astcart.Cart_items.adapter = Ember.FixtureAdapter.create();
于 2013-09-08T17:16:01.633 回答