# jsonapi-datastore
[![Build Status](https://travis-ci.org/beauby/jsonapi-datastore.svg)](https://travis-ci.org/beauby/jsonapi-datastore)
JavaScript client-side [JSON API](http://jsonapi.org) data handling made easy.
Current version is v0.4.0-beta. It is still a work in progress, but should do what it says.
## Description
The [JSONAPI](http://jsonapi.org) standard is great for exchanging data (which is its purpose), but the format is not ideal to work directly with in an application.
jsonapi-datastore is a JavaScript framework-agnostic library (but an [AngularJS version](#angularjs) is provided for convenience) that takes away the burden of handling [JSONAPI](http://jsonapi.org) data on the client side.
What it does:
- read JSONAPI payloads,
- rebuild the underlying data graph,
- allows you to query models and access their relationships directly,
- create new models,
- serialize models for creation/update.
What it does not do:
- make requests to your API. You design your endpoints URLs, the way you handle authentication, caching, etc. is totally up to you.
## Installing
Install jsonapi-datastore with `bower` by running:
```
$ bower install jsonapi-datastore
```
or with `npm` by running:
```
$ npm install jsonapi-datastore
```
## Parsing data
Just call the `.sync()` method of your store.
```javascript
var store = new JsonApiDataStore();
store.sync(data);
```
This parses the data and incorporates it in the store, taking care of already existing records (by updating them) and relationships.
## Parsing with meta data
If you have meta data in your payload use the `.syncWithMeta` method of your store.
```javascript
var store = new JsonApiDataStore();
store.syncWithMeta(data);
```
This does everything that `.sync()` does, but returns an object with data and meta split.
## Retrieving models
Just call the `.find(type, id)` method of your store.
```javascript
var article = store.find('article', 123);
```
or call the `.findAll(type)` method of your store to get all the models of that type.
```javascript
var articles = store.findAll('article');
```
All the attributes *and* relationships are accessible through the model as object properties.
```javascript
console.log(article.author.name);
```
In case a related resource has not been fetched yet (either as a primary resource or as an included resource), the corresponding property on the model will contain only the `type` and `id` (and the `._placeHolder` property will be set to `true`). However, the models are *updated in place*, so you can fetch a related resource later, and your data will remain consistent.
## Serializing data
Just call the `.serialize()` method on the model.
```javascript
console.log(article.serialize());
```
## Examples
```javascript
// Create a store:
var store = new JsonApiDataStore();
// Then, given the following payload, containing two `articles`, with a related `user` who is the author of both:
var payload = {
data: [{
type: 'article',
id: 1337,
attributes: {
title: 'Cool article'
},
relationships: {
author: {
data: {
type: 'user',
id: 1
}
}
}
}, {
type: 'article',
id: 300,
attributes: {
title: 'Even cooler article'
},
relationships: {
author: {
data: {
type: 'user',
id: 1
}
}
}
}]
};
// we can sync it:
var articles = store.sync(payload);
// which will return the list of synced articles.
// Later, we can retrieve one of those:
var article = store.find('article', 1337);
// If the author resource has not been synced yet, we can only access its id and its type:
console.log(article.author);
// { id: 1, _type: 'article' }
// If we do sync the author resource later:
var authorPayload = {
data: {
type: 'user',
id: 1,
attributes: {
name: 'Lucas'
}
}
};
store.sync(authorPayload);
// we can then access the author's name through our old `article` reference:
console.log(article.author.name);
// 'Lucas'
// We can also serialize any whole model in a JSONAPI-compliant way:
console.log(article.serialize());
// ...
// or just a subset of its attributes/relationships:
console.log(article.serialize({ attributes: ['title'], relationships: []}));
// ...
```
## Documentation
See [DOCUMENTATION.md](DOCUMENTATION.md).
## What's missing
Currently, the store does not handle `links` attributes or resource-level or relationship-level meta.
## Notes
### AngularJS
jsonapi-datastore is bundled with an AngularJs wrapper. Just include `ng-jsonapi-datastore.js` in your `index.html` and require the module `beauby.jsonApiDataStore` in your application.
You can then use the `JsonApiDataStore` factory, which is essentially defined as follows:
```javascript
{
store: new JsonApiDataStore(),
Model: JsonApiDataStoreModel
}
```
so that you can use it as follows:
```javascript
angular
.module('myApp')
.controller('myController', function(JsonApiDataStore) {
var article = JsonApiDataStore.store.find('article', 1337);
var newArticle = new JsonApiDataStore.Model('article');
newArticle.setAttribute('title', 'My cool article');
console.log(newArticle.serialize());
});
```
## Contributing
All pull-requests welcome!
没有合适的资源?快使用搜索试试~ 我知道了~
商城d小程序源码.zip
共269个文件
rb:70个
zip:40个
png:30个
需积分: 9 3 下载量 160 浏览量
2020-02-20
19:36:42
上传
评论
收藏 41.38MB ZIP 举报
温馨提示
一些商城小程序源码供大家学习 有茶叶 有电商 等 网上找的 是学习小程序参考 给大家 在
资源推荐
资源详情
资源评论
收起资源包目录
商城d小程序源码.zip (269个子文件)
bundle 129B
Capfile 1KB
demo导入说明.docx 182KB
mailer.html.erb 229B
mailer.text.erb 13B
Gemfile 2KB
.gitignore 947B
.gitignore 29B
favicon.ico 0B
address_data.js 49KB
jsonapi-datastore.js 8KB
index.js 5KB
index.js 4KB
index.js 3KB
cart.js 3KB
index.js 3KB
address.js 2KB
index.js 2KB
util.js 2KB
app.js 2KB
index.js 2KB
index.js 2KB
statistics.js 2KB
index.js 2KB
category.js 1KB
httpHelper.js 1KB
product.js 1KB
show_product.js 1KB
index.js 986B
index.js 711B
app.js 710B
address.js 697B
logs.js 280B
mine.js 226B
config.js 140B
package.json 3KB
app.json 1KB
app.json 1KB
index.json 86B
index.json 86B
index.json 83B
index.json 83B
index.json 83B
index.json 83B
index.json 83B
index.json 77B
logs.json 56B
mine.json 49B
index.json 49B
address.json 49B
cart.json 46B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
.keep 0B
Gemfile.lock 9KB
LICENSE.md 1KB
README.md 952B
README.md 533B
README.md 5KB
53_asset_info.png 453KB
5_asset_info.png 444KB
3_asset_info.png 432KB
53_assets.png 176KB
3_assets.png 166KB
5_assets.png 149KB
add.png 6KB
ding.png 5KB
asset.png 5KB
dd.png 5KB
our_select.png 4KB
packing.png 4KB
direct_sale.png 4KB
dingdan.png 3KB
1.png 3KB
cart_active.png 2KB
cart.png 2KB
ba_active.png 2KB
volume_small.png 2KB
profile_active.png 2KB
ba.png 2KB
profile.png 2KB
new_small.png 2KB
hot_small.png 1KB
dianpu.png 1KB
gouwuche.png 1KB
user.png 1KB
dianpu_s.png 971B
gouwuche_s.png 922B
user_s.png 736B
rails 264B
rake 213B
Rakefile 227B
共 269 条
- 1
- 2
- 3
资源评论
ansoft888
- 粉丝: 3
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 从XML生成可与Ajax共同使用的JSON中文WORD版最新版本
- silverlight通过WebService连接数据库中文WORD版最新版本
- 使用NetBeans连接SQLserver2008数据库教程中文WORD版最新版本
- XPath实例中文WORD版最新版本
- XPath语法规则中文WORD版最新版本
- XPath入门教程中文WORD版最新版本
- ORACLE数据库管理系统体系结构中文WORD版最新版本
- Sybase数据库安装以及新建数据库中文WORD版最新版本
- tomcat6.0配置oracle数据库连接池中文WORD版最新版本
- hibernate连接oracle数据库中文WORD版最新版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功