ExtJS4 - 将多个模型添加到商店然后调用store.sync不会保存所有数据

时间:2015-01-19 09:40:47

标签: extjs extjs4 extjs4.2

美好的一天,我正在使用ExtJS4构建一个Web应用程序,我在那里创建了多个模型,然后将它们添加到商店然后同步商店。但是,在调用store.sync()时,我看到一个空白的数据库条目。

我的代码是这样的:

this.mon(uploadDialog, 'uploadcomplete', function(uploadPanel, manager, items, errorCount) {

    var itemCount = items.length;

    for(var i = 0 ; i < itemCount ; i++){
        pathArray.push(items[i].getName());
    }

    console.log('---------- pathArray count = ' + pathArray.length);

    var galleryStore = Ext.getStore('userProfileGallery');
    var userStore = Ext.getStore('userStore');
    var userModel = userStore.first();

    //------------ Iterate through the items, create a model, then add model to the store

    for(var j = 0 ; j < itemCount ; j++){
        var personPhotoModel = Ext.ModelManager.create({
        }, 'myappName.model.userPhoto');

        var currentdate = new Date();

        var uploadDate = currentdate.getDate() + "/" +
            (currentdate.getMonth()+1)  + "/" +
            currentdate.getFullYear();

        var uploadTime = currentdate.getHours() + ":" +
            currentdate.getMinutes() + ":" +
            currentdate.getSeconds();

        personPhotoModel.set("image_path", "gallery/" + pathArray[j]);
        personPhotoModel.set("description", "Gallery Image");
        personPhotoModel.set("upload_date", uploadDate);
        personPhotoModel.set("upload_time", uploadTime);
        personPhotoModel.set("user_id", userModel.get('user_id'));

        galleryStore.add(personPhotoModel);
        console.log('gallery store count = ' + galleryStore.count());

    }

    //---------- sync the store here

    console.log('gallery store count = ' + galleryStore.count());

    galleryStore.sync();

}, this);

到目前为止我所尝试的只是通过仅上传一个文件来制作1个模型并且工作得很好。但是,当我向商店添加多个模型然后同步商店时,无论我添加到模型中的项目数量多少,我都会得到一个空白行。

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

我没有直接看到为什么你的代码没有按预期工作,但我看到了很大的改进空间:

  • Ext.ModelManager.create 已弃用使用Ext.create('Your Model', data);
  • 应用程序名称和类名以大写字母开头!
  • 避免在循环中声明变量(循环时日期不会改变......)
  • 在创建模型时声明日期(更快),否则使用record.beginEdit()record.endEdit()进行批量修改。这可以防止每个set
  • 触发事件

以下是我要做的事情:

this.mon(uploadDialog, 'uploadcomplete', function (uploadPanel, manager, items, errorCount) {

    var itemCount = items.length,
        now = new Date(),
        uploadDate = Ext.Date.format(now, 'd/m/Y'),
        uploadTime = Ext.Date.format(now, 'H:i:s'),
        i;

    for (i = 0; i < itemCount; i++) {
        pathArray.push(items[i].getName());
    }

    console.log('---------- pathArray count = ' + pathArray.length);

    var galleryStore = Ext.getStore('userProfileGallery'),
        userStore = Ext.getStore('userStore'),
        user = userStore.first();

    //------------ Iterate through the items, create a model, then add model to the store

    for (i = 0; i < itemCount; i++) {

        galleryStore.add(Ext.create('MyappName.model.UserPhoto', {
            image_path: "gallery/" + pathArray[i],
            description: "Gallery Image",
            upload_date: uploadDate,
            upload_time: uploadTime,
            user_id: user.get('user_id')
        }));

        console.log('gallery store count = ' + galleryStore.count());
    }

    //---------- sync the store here

    console.log('gallery store count = ' + galleryStore.count());

    galleryStore.sync();

}, this);

如果这不起作用,则您的错误不在此代码中,而是在商店,模型或后端