繁体   English   中英

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

[英]ExtJS4 - Adding multiple model to a store then calling store.sync does not save all the data

美好的一天,我正在使用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个模型,并且效果很好。 但是,当我向商店中添加多个模型并同步商店时,无论我添加了多少项目,都会得到一个空白行。

很感谢任何形式的帮助。 谢谢。

我看不到为什么您的代码无法按预期运行,但是我看到很多改进的余地:

  • 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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM