javascript - JS 前端各框架有什么值得推荐的数据持久层框架?
阿神
阿神 2017-04-10 17:04:51
[JavaScript讨论组]

现在主流的框架各有什么推荐的数据持久层推荐的?

我理想中的持久层应该是这样的:

// Adapter 是用来和后台(服务器,Local Storage,或者别的持久性服务)联系的
Const PublicAdapter = new Adapter({
    fetchAll: (modelCls)=>{}, // a function to fetch all data via ajax or local storage
    fetch: (modelCls)=>{},
    create: (modelCls, configuration)=>{},
    destroy: (modelInstance)=>{}
});

// 数据模型
Const User = new Model({
    id: DataType('number'),
    email: DataType('string',{'pattern':/\w+@\w+\.\w+)/}),
})

Const ModelName1 = New Model({
    id: DataType('number'),
    date: DataType('date',{'format':'YYYY-MM-DD', 'default':'now'}),
    active: DataType('boolean',{'default':true}),
    users: DataType('oneToMany',{ref:User}),
    parent: DataType('ManyToOne')
    _adapter: PublicAdapter.extend({
        destroy:()=>{} // 覆盖 PublicAdapter 的删除方法,比如先删除所有关联的 User 对象再删除自己
    })
});

// Model 至少应该提供两个方法
Model.save() // 调用 adpater 的对应方法提交数据到后台,自动决定使用 post 还是 put 方法
Model.destroy()


// 一个好用的持久层框架还应该有 store 来提供一些批量化处理的办法

// list 
this.store.fetchAll(ModelName1,{minId:1, maxId:1000})
    .then(data=>console.log('all models between 1 and 100 are fetched'));


// create
this.store
    .create(ModelName1, {id:5, date: Date.now(), active: 1})  // return created model
    .save()
    .then(model=>
        console.log('model saved, might call adapter saved to remote server or save to local storage when network is not available')
     )

// get 
this.store.fetch(ModelName1, {id:1}, {nocache:true}) // 无视 store 中的缓存
    .then(data=>{ console.log('data fetched,added to store') })

// delete
this.store.destroy(modelInstance)
    .then(result=>console.log('model destroyed at remote server and store '))

下面是一些我知道的市面上针对各框架的持久层,肯定还有遗漏,希望各位补充一下。也想问一下, 有没有独立的类似的数据持久层?

  • Ember:

    • Ember data
      http://emberjs.com/api/data/

  • AngularJs:

    • ngActiveResource
      https://github.com/FacultyCreative/ngActiveResource

    • ng2.0 data persistence design doc
      https://docs.google.com/document/d/1DMacL7iwjSMPP0ytZfugpU4v0PWUK0BT6lhyaVEmlBQ/

  • React:

    • Relay 算吗???

  • Vue

    • ???

阿神
阿神

闭关修行中......

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号