0

我想知道将数据加载到存储中的正确方法是什么。我将路由器定义为:

this.resource('analyticsTemplates', { path: 'templates' });
this.resource('analyticsTemplate', { path: 'templates/:analytics_template_id' });

对于我的“analyticsTemplates”路线,我定义了一个车把模板,该模板列出了表中的所有模板和一个添加按钮,该按钮打开了一个带有表单的模式框,提交时应该向商店添加一条新记录并更新我的表。现在我的表单使用文件输入字段,所以我没有使用

this.get('model').save()

正确同步商店。相反,当表单提交时,它将表单发布到隐藏的 iframe,然后这个 iframe 的正文内容由代表当前添加的记录的 json 数据填充。返回数据如下所示:

{
    "analyticsTemplate": {
        "id": 6,
        "name": "deee",
        "description": "asdasdasd",
        "csFileName": "commandScript.txt",
        "csLastUpdatedBy": "Deewendra Shrestha",
        "csLastUpdated": "2013-10-22T18:00:54Z",
        "dateCreated": "2013-10-22T18:00:54Z",
        "lastUpdated": "2013-10-22T18:00:54Z",
        "createdBy": "Deewendra Shrestha",
        "lastUpdatedBy": "Deewendra Shrestha",
        "parameters": {
            "homeDir": {
                "category": "global",
                "description": "Where is the analytics folder",
                "name": "homeDir",
                "value": "Q:/Reckitt Benckiser/2013/RB_Steroid/Analytics/IntergerateTest_DONOTUSE"
            },
            "historyDir": {
                "category": "global",
                "description": "Where R image will be saved",
                "name": "historyDir",
                "value": "Rhistory"
            },
            "Sourcefolder": {
                "category": "type 1",
                "description": "Where are source codes and functions are saved",
                "name": "Sourcefolder",
                "value": "J:/_MRO/Analytics/Analytics_R_Source_Scripts"
            },
            "resultsDir": {
                "category": "global",
                "description": "Where are results stored",
                "name": "resultsDir",
                "value": "results"
            },
            "dataDir": {
                "category": "opt",
                "description": "Where is raw csv stored",
                "name": "dataDir",
                "value": "Export"
            },
            "testParam": {
                "category": "global",
                "description": "some long sentence repeated multiple times. some long sentence repeated multiple times. some long sentence repeated multiple times.",
                "name": "testParam",
                "value": "some long sentence repeated multiple times. some long sentence repeated multiple times.some long sentence repeated multiple times."
            }
        }
    }
}

我在控制器中尝试了两件事:

this.get('store').push('analyticsTemplate',jsonData["analyticsTempalte"]);

还 :

var newRecord = this.get('store').createRecord('analyticsTemplate');
for(var key in jsonData["analyticsTemplate"]){
    o.set(key,jsonData["analyticsTemplate"][key]);
}

使用上面的任何一种方法,我都可以看到表中列出的新记录,但是当我单击记录以深入了解“analyticsTemplate”路线(类似于从帖子->帖子)时,我收到 js 错误:

parameters.forEach(function (parameter) {
    discreteCategories[parameter.get('category')] = 1;
});

我已经定义了一个 DS.transform 来将参数转换为 ArrayProxy 并且我认为当我加载记录时不会执行此转换,因此子路由不知道如何处理参数。那么在这种情况下我该如何应用转换呢?

我希望我可以申请:

this.get('model').reload()

但似乎这适用于 ObjectController 上下文,而不适用于 ArrayController。有什么想法/替代方案的朋友吗?

4

1 回答 1

0

Jeez ember 让事情变得如此简单,我试图找到困难的方法。所以我所要做的就是在我的控制器动作中添加这些:

ajaxFileUpload: {
     var self = this;
    //do upload stuff, add record to DB
    .....
    var analyticsTemplateData = $.parseJSON(responseText);
    var id = analyticsTemplateData["analyticsTemplate"]["id"];
    var store =self.get('store');
    store.find('analyticsTemplate',id);
}

这将花费我认为还不错的服务器请求,并完成工作。但是,如果您知道另一种加载数据的方法而无需额外的服务器调用,那么我会全力以赴。谢谢。

于 2013-10-22T20:12:12.817 回答