使用Sencha Touch处理复杂的业务规则

时间:2011-10-13 10:41:21

标签: sencha-touch extjs

我感谢大家给我的帮助和支持,开始使用Sencha Touch。现在我进入了一些复杂的业务场景处理,我觉得处理起来要困难得多。这是我的业务需求。

  1. 成功登录应用程序后,sencha客户端会收到JSON对象响应,其中包含所有可用的产品和代码。此外,states标记包含排除产品列表。我在下面粘贴的JSON格式示例。

  2. 我必须有一个表单,我必须在其中显示状态和产品下拉框。 UI部分很好。我已经完成了它。 但现在复杂性是当我选择一个州时,我必须从总的可用产品中排除产品清单。

  3. 我已使用 Ajax请求,然后使用解码方法解码JSON响应。现在我将完整的JSON作为对象。

  4. 我有2个商店,一个用于产品列表,另一个用于状态列表。 我不要求我的任何商店与服务器同步,因为它会增加数据使用量。

  5. 选项控制的监听器也已完成,现在如何操作JSON并动态更新产品Store。请提出建议。

  6. 以下是JSON响应示例:

    {   
        "defaultProducts": {
            "product": [
                {
                    "code": "pro1",
                    "name": "Product AA"
                }, {
                    "code": "pro1",
                    "name": "Product BB"
                }, {
                    "code": "pro1-INP",
                    "name": "Product CC"
                }, {
                    "code": "uni1-sc",
                    "name": "Product DD"
                }, {
                    "code": "pro1",
                    "name": "Product EE"
                }, {
                    "code": "uni1-sc",
                    "name": "TCO - Enhanced"
                }, {
                    "code": "uni1",
                    "name": "Product FF"
                }, {
                    "code": "uni1",
                    "name": "Product GG"
                }
            ]
        },
        "states": {
            "state": [
                {
                    "excludeProducts": {
                        "excludeProduct": [
                            {
                                "code": "pro1",
                                "name": "Product BB"
                            }, {
                                "code": "pro1-INP",
                                "name": "Product CC"
                            }, {
                                "code": "pro1",
                                "name": "Product EE"
                            }, {
                                "code": "uni1",
                                "name": "Product FF"
                            }, {
                                "code": "uni1",
                                "name": "Product GG"
                            }
                        ]
                    },
                    "code": "AL",
                    "name": "Alabama"
                }, {
                    "excludeProducts": {
                        "excludeProduct": [
                            {
                                "code": "pro1",
                                "name": "Product BB"
                            }, {
                                "code": "pro1-INP",
                                "name": "Product CC"
                            }, {
                                "code": "pro1",
                                "name": "Product EE"
                            }, {
                                "code": "uni1",
                                "name": "Product FF"
                            }, {
                                "code": "uni1",
                                "name": "Product GG"
                            }
                        ]
                    },
                    "code": "AK",
                    "name": "Alaska"
                }, {
                    "excludeProducts": {
                        "excludeProduct": [
                            {
                                "code": "pro1",
                                "name": "Product BB"
                            }, {
                                "code": "pro1-INP",
                                "name": "Product CC"
                            }, {
                                "code": "uni1-sc",
                                "name": "Product DD"
                            }, {
                                "code": "pro1",
                                "name": "Product EE"
                            }, {
                                "code": "uni1-sc",
                                "name": "TCO - Enhanced"
                            }
                        ]
                    },
                    "code": "AZ",
                    "name": "Arizona"
                }, {
                    "excludeProducts": {
                        "excludeProduct": [
                            {
                                "code": "pro1",
                                "name": "Product BB"
                            }, {
                                "code": "pro1-INP",
                                "name": "Product CC"
                            }, {
                                "code": "pro1",
                                "name": "Product EE"
                            }, {
                                "code": "uni1",
                                "name": "Product FF"
                            }, {
                                "code": "uni1",
                                "name": "Product GG"
                            }
                        ]
                    },
                    "code": "AR",
                    "name": "Arkansas"
                }
            ]
        },
        "licensedTo": "mycomp.com"
    }
    

    请指导我用于实现此类复杂方案的任何其他逻辑。

1 个答案:

答案 0 :(得分:0)

您应该将侦听器附加到状态下拉框,然后根据要排除的项目将过滤器添加到产品列表中。

这是状态下拉框中的更改侦听器应如下所示:

  listeners: {
        change:function(selectfield, value){
            //here you should get reference to the excludeProduct array
            var exProductArray = [];

            //then add filter to the product store like this
            productStore.clearFilter(false); // clears previous filters

            for(var i=0; i<exProductArray.length;++i){
              productStore.filter("code",exProductArray[i].code);       
            }              
        }
   },

要获取对productArray的引用,可以将valueField设置为状态下拉列表作为code属性,并将状态模型的idProperty设置为code属性。然后你可以从状态存储中获取状态记录,如下所示:

  var stateRecord = stateStore.getById(value);

value对象change:function(selectfield, value){的值。然后,在获得正确的stateRecord以获取对productArray的引用后,您应该这样做:

 var exProductArray = stateRecord.excludeProducts.excludeProduct;
相关问题