ãã«ã¹ã±ã¢äºæ¥é¨ã®æ¿±ç°ã§ããè±ç²ãã¤ããææã§ãããã¿ãªãã楽ããéçºãã¦ã¾ããï¼
ããããå¥åº·ã§ã¯ãJavaScriptï¼ä»¥ä¸ JSï¼ã§éåæã«ãµã¼ãå´ã®ãªã½ã¼ã¹æä½ãè¡ãéã«ãjs-data ã¨ããã©ã¤ãã©ãªã使ã£ã¦ãã¾ããRails ã¦ã¼ã¶ã«ã¨ã£ã¦ã¯ã¨ã£ã¤ãããã便å©ãªã©ã¤ãã©ãªã§ãããæ¥æ¬èªã§ã®æ å ±ãã»ã¨ãã©è¦å½ãããªãã£ããããç°¡åã«ãç´¹ä»ãããã¨æãã¾ãã
js-data ã¨ã¯
JS 製ã®ãã¼ã¿ç®¡çç¨ã©ã¤ãã©ãªã§ããRESTful API ãªã©ãéãã¦åå¾ã§ãããã¼ã¿ï¼ãªã½ã¼ã¹ï¼ãæ½è±¡åãã¦ãCRUD æä½ãçµ±ä¸ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã§è¡ããããã«ãã¦ããã¾ãã
ãªã½ã¼ã¹ãã¨ã«ãªãã¸ã§ã¯ãï¼ã¢ãã«ï¼ãå®ç¾©ããããfind
ã§ãã¼ã¿ãåã£ã¦ãããªã©ãActiveRecord ãªã©ã® O/R ãããã®ãããªä½¿ãåæãç¹å¾´ãè¨è¨çã«ã¯ Ember data ã®å½±é¿ãåãã¦ãã¾ãã
以ä¸ã§ãåºæ¬çãªä½¿ãæ¹ãè¦ã¦ã¿ã¾ãããã
ãªã½ã¼ã¹ã¨ãã¼ã¿ã¹ãã¢ã®å®ç¾©
ãªã½ã¼ã¹ã¨ã¯ãActiveRecord ã§è¨ãã°ã²ã¨ã¤ã²ã¨ã¤ã®ã¢ãã«ã«ããã JS ãªãã¸ã§ã¯ãã§ã以ä¸ã®ãããªæ©è½ãæã£ã¦ãã¾ãã
- ãªã½ã¼ã¹åã対å¿ãã API ã®ã¨ã³ããã¤ã³ãã® URL ãªã©ã®ã¡ã¿ãã¼ã¿ç®¡ç
- ãªã½ã¼ã¹ã«å¯¾ãã CRUD ç¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®æä¾
API ãªã©ãéãã¦èªã¿è¾¼ã¾ãããã¼ã¿ã¯ä¸æ¦ãã¼ã¿ã¹ãã¢ã¨å¼ã°ãããªã³ã¡ã¢ãªãã£ãã·ã¥ã«æ ¼ç´ããã¾ãã ä¸åº¦èªã¿è¾¼ãã ãã¼ã¿ã¯ã¹ãã¢å ããå¼ã³åºããUpdate, Delete ãªã©ã®æ´æ°ç³»æä½ã®ã¿ã¤ãã³ã°ã§ãµã¼ãã«ãã¼ã¿ãæ¸ãæ»ãã¨ããããã§ãã
ä¾ - Book ãªã½ã¼ã¹
ä¾ã¨ãã¦ãä¸åã®æ¬ã表ã Book ãªã½ã¼ã¹ãå®ç¾©ãã¦ã¿ã¾ãããã
// ãã¼ã¿ã¹ãã¢ãä½æ var store = new JSData.DS(); // å ãã¼ã¿ã®åç §å ã¨ã㦠HTTP (Web API) ãæå®ãã store.registerAdapter('http', new DSHttpAdapter(), { default: true }); // ãã¼ã¿ã¹ãã¢ä¸ã« Book ãªã½ã¼ã¹ãå®ç¾© var Book = store.defineResource({ name: 'book', // ãªã½ã¼ã¹å endpoint: 'books' // 対å¿ãã API ã®ã¨ã³ããã¤ã³ã });
RESTful API ã®æºå
ä¸è¨ã®ããã«å®ç¾©ãããªã½ã¼ã¹ãªãã¸ã§ã¯ãã使ãã«ã¯ãæä½ãå®éã«è¡ãããã® Web API ãç¨æãã¦ããå¿
è¦ãããã¾ãããªã½ã¼ã¹å®ç¾©ã«ã¯ endpoint
ã¨ãã API ã®ã¨ã³ããã¤ã³ãã®æ
å ±ããå«ã¾ãã¦ãã¾ãããããã® URL ã« json ãªãã¸ã§ã¯ããè¿å´ãã RESTful 㪠API ãå®ç¾©ãã¦ããã°ãjs-data ãèªåçã«å©ãã¦ãããããã«ãªã£ã¦ãã¾ãã
å®ç¾©ãã¦ããæä½
以ä¸ã®ãã㪠API ãåå¨ãããã¨ãæå¾ ããã¦ãã¾ããå¿ ãããå ¨é¨å®è£ ãã¦ããå¿ è¦ã¯ããã¾ããã使ç¨ããããã®ã ã㧠OK ã§ãã
operation | URL | HTTP method | Description |
---|---|---|---|
Create | /<resource> |
POST | æ°ããã¢ã¤ãã ã®ä½æãããããã£ï¼{ title: 'ç ãç' } ï¼ãåãåããä½æããã¢ã¤ãã ã®ãã¼ã¿ãè¿ãï¼{ id: 1, name: 'ç ãç' } ï¼ã |
Read | /<resource>/:id |
GET | æ¢åã®ã¢ã¤ãã ã®åå¾ã { id: 1, title: "ç ãç" } |
Update | /<resource>/:id |
PUT | æ¢åã®ã¢ã¤ãã ã®æ´æ°ãããããã£ï¼{ title: 'ç¶ã®è©«ã³ç¶' } ãªã©ï¼ãåãåããæ´æ°ããã¢ã¤ãã ãè¿ãï¼{ id: 1, title: 'ç¶ã®è©«ã³ç¶' } ï¼ã |
Delete | /<resource>/:id |
DELETE | æ¢åã®ã¢ã¤ãã ã®åé¤ã |
Read All | /<resource> |
GET | æå®ãããæ¡ä»¶ã§ãªã½ã¼ã¹ãæ¤ç´¢ããé
åã§è¿ãï¼[{ id: 1, title: 'ç¶ã®è©«ã³ç¶' }, { id: 1, title: "ç ãç" }] ï¼ãæ¤ç´¢æ¡ä»¶ã¯ GET ãã©ã¡ã¼ã¿ã¨ãã¦æå®ã |
Update All | /<resource> |
PUT | æå®ãããæ¡ä»¶ã§æ¤ç´¢ãããè¤æ°ã®ãªã½ã¼ã¹ãä¸æ¬æ´æ°ãæ´æ°ããããªã½ã¼ã¹ã®é åãè¿ãã |
Delete All | /<resource> |
DELETE | æå®ãããæ¡ä»¶ã§æ¤ç´¢ãããè¤æ°ã®ãªã½ã¼ã¹ãä¸æ¬åé¤ã |
ãªã½ã¼ã¹ã® CRUD ã¤ã³ã¿ã¼ãã§ã¤ã¹
ãã¼ã¿ã¹ãã¢ï¼ãªã½ã¼ã¹ã®å®ç¾©ã対å¿ãã API ã®å®è£ ãçµããã°ãå®éã«ãªã½ã¼ã¹ã®æä½ãè¡ããã¨ãã§ããããã«ãªãã¾ãã
// Read var book = null; Book.find(1).then(function (data) { book = data; book; // { id: 1, title: "ç ãç", publishedIn: "1979" } }); // Create var book = null; Book.create({ title: "ç¶ã®è©«ã³ç¶", publishedIn: "1988" }).then(function (data) { book = data; book; // { id: 2, name: "ç¶ã®è©«ã³ç¶", publishedIn: "1988" } }); // Update Book.update(2, { publishedIn: "1978" }).then(function (data) { data; // { id: 2, name: "ç¶ã®è©«ã³ç¶", publishedIn: "1978" } }); // Read All var books = []; Book.findAll({ // æ¤ç´¢æ¡ä»¶ã®æå®ã // get ãã©ã¡ã¼ã¿ã® "?where=..." ã« JSON æååã¨ãã¦åãè¾¼ã¾ããã®ã§ã // ãµã¼ãã§ã¯ããããã¼ã¹ãã¦é åã«ããå¾ãæ¤ç´¢æ¡ä»¶ã«ä½¿ç¨ãã where: { publishedIn: "1979" } }).then(function (data) { books = data; // é åãè¿ãã[{ id: 1, title: "ç ãç", publishedIn: "1979" }] });
ãã¤ã³ã
éåæãªã¡ã½ãã㯠Promise ãè¿ã
API ã³ã¼ã«ãä¼´ãæä½ã¯éåæã«è¡ãããPromise ãã¿ã¼ã³ã使ã£ã¦ã¡ã½ãããã§ã¤ã³ããªãããã¼ã¿ãåãåããã¨ã«ãªãã¾ãã
ãã¼ã¿ã®æå®ãããã·ã³ã¿ã¯ã¹ããã
findAll
ã destroyAll
ãªã©ãè¤æ°ã®ãã¼ã¿ãæä½ããã¡ã½ããã«ã¯ãã¼ã¿ãæå®ããç¬èªã®ã¯ã¨ãªã·ã³ã¿ã¯ã¹ãç¨æããã¦ãã¾ããwhere
ã orderBy
ãªã©ãSQL ã使ã£ããã¨ããã人ãªã馴æã¿ããããã®ã°ããã§ãããããã ãããã®ã¯ã¨ãªãåãä»ããããã®å®è£
ããµã¼ãã«å ããå¿
è¦ãããã®ã§ã使ããªãã¨ããé¸æè¢ãå¯è½ã§ãã
ãã®ã·ã³ã¿ã¯ã¹ã¯ããã¼ã¿ã¹ãã¢ã®ä¸ã«ãããã¼ã¿ã®ã¿ã対象ã«æ¤ç´¢ãè¡ã Resource.filter
ã¡ã½ãããªã©ã§ãå
±éã§ä½¿ãã¾ãã
2度ç®ä»¥éã®åå¾ã¯ç´æ¥ãã¼ã¿ã¹ãã¢ãã
ä¸åº¦èªã¿è¾¼ãã ãã¼ã¿ã¯ãã¼ã¿ã¹ãã¢ã«ãã£ãã·ã¥ããã¦ãã¾ããfind
ãè¤æ°åè¡ã£ã¦ããAPI ã³ã¼ã«ãçºçããã®ã¯æåã®ä¸åº¦ã§æ¸ãã®ã§å¹ççã§ãããä½ããã®çç±ã§å¼·å¶çã«ãã¼ã¿ãèªã¿è¾¼ã¿ç´ãããã¨ãã¯ã以ä¸ã®ããã«ãªãã·ã§ã³ã渡ãã° OK ã§ãã
book; // { id: 1, title: "ç ãç" } Book.find(1, { bypassCache: true // ãã®ãªãã·ã§ã³æå®ã§ãã£ãã·ã¥ã使ãããªã½ã¼ã¹ãèªã¿è¾¼ã¿ç´ã }).then(function (data) { data; // { id: 1, title: "ç ãç" } })
ãªã¬ã¼ã·ã§ã³ã®å®ç¾©
ãªã½ã¼ã¹å士ã®éã«é¢é£ãå®ç¾©ãã¦ããã¼ã¿ã¹ãã¢å ã§ã²ãä»ãã¦ãããã¨ãã§ãã¾ãã
ä¾ - Author ãªã½ã¼ã¹
ä¾ã¨ãã¦å ã»ã©ã® Book ã«å¯¾ãã Author ãªã½ã¼ã¹ãå®ç¾©ããé¢é£ãå®ç¾©ãã¦ã¿ã¾ãããã
// ãã¼ã¿ã¹ãã¢ä¸ã« Author ãªã½ã¼ã¹ãå®ç¾© Author = store.defineResource({ name: 'author', endpoint: 'authors', relations: { hasMany: { book: { // author.books ã§ã¢ã¯ã»ã¹ localField: 'books', // book ãªã½ã¼ã¹ã«ããããauthor ãåç §ããããã®å¤é¨ãã¼ç¸å½ã®ããããã£ãæå® foreignKey: 'authorId' } } } }); // Books ãªã½ã¼ã¹ã®å®ç¾©ãå¤æ´ Book = store.defineResource({ name: 'book', endpoint: 'books', relations: { belongsTo: { author: { // book.author ã§ã¢ã¯ã»ã¹ localField: 'author', // book ãªã½ã¼ã¹ã«ããããauthor ãåç §ããããã®å¤é¨ãã¼ç¸å½ã®ããããã£ãæå® localKey: 'authorId' } } } });
defineResource
ãå¼ã¶ã¨ãã«ããªã¬ã¼ã·ã§ã³ç¨ã®ãªãã·ã§ã³ã渡ãã ãã§ãããã¼ã¿ã¹ãã¢ã«å
¥ã£ããªãã¸ã§ã¯ãã¯ãlocalKey/foreignKey
ããã¨ã«é¢é£ä»ããããlocalField
ã«è¨å®ããååã§ã¢ã¯ã»ã¹ã§ããããã«ãªãã¾ãã
é¢é£ãªã½ã¼ã¹ã®æä½
é¢é£ãå®ç¾©ãããªã½ã¼ã¹ãå®éã«æä½ãã¦ã¿ã¾ãããã
authorResponse = { name: "åç°é¦å", birthDate: "1929-11-28", books: [ { id: 1, authorId: 1, title: "ç ãç", publishedIn: "1979" }, { id: 2, authorId: 1, title: "ç¶ã®è©«ã³ç¶", publishedIn: "1978" } ] } // inject ã¯ãã¼ã¿ã¹ãã¢ã¸ç´æ¥ãªã½ã¼ã¹ãæ¿å ¥ããã¡ã½ãã // ï¼API çµç±ã® create ã¯è¡ãããªãï¼ author = Author.inject(authorResponse); // { name: "åç°é¦å", birthDate: "1929-11-28" } author.books; // [{ id: 1, authorId: 1, name: "ç ãç", publishedIn: "1979" }, { id: 2, ... }] book = null; // ãã§ã«ãã¼ã¿ã¹ãã¢ã«æ¿å ¥ããã¦ããã®ã§ãAPI ã³ã¼ã«ã¯èµ·ãããªãã { id: 1, authorId: 1, name: "ç ãç", publishedIn: "1979" } Book.find(2).then(function (data) { book = data }); Book.create({ authorId: author.id, title: "é¿ä¿®ç¾ ã®ãã¨ã", publishedIn: "1979" }).then(function(data){ // é¢é£ãªã½ã¼ã¹ãå¼ã³åºãã¨ãæ°ããã¤ã³ã¹ã¿ã³ã¹ãå«ã¾ãã¦ãã author.books; // [{ id: 1, ...}, { id: 2, ... }, { id: 3, name: "é¿ä¿®ç¾ ã®ãã¨ã", ...}] });
ãã¤ã³ã
API ã¯é¢é£ãã¼ã¿ãä¸æ°ã«è¿å´ã§ãã
ä¸è¨ã®ä¾ã§ãæ°ã¥ãã ã¨æãã¾ãããã¬ã¹ãã³ã¹ã«é¢é£ã¢ãã«ã®ãã¼ã¿ãå«ãã¦ããã°ãèªåçã«ãã¼ã¿ã¹ãã¢ã¸ã®ç»é²ãè¡ããã¾ãããã®å ´åãauthor.books
ã®ã¢ã¯ã»ã¹ãããéã«ã API ã³ã¼ã«ã¯è¡ãããããã¼ã¿ã¹ãã¢å
ã®ãã¼ã¿ãè¿ã£ã¦ãã¾ããé¢é£ãã¼ã¿ããã¹ããã¦è¿ãã¦ã js-data ã®ãªã½ã¼ã¹ã¨ãã¦æ±ãããã®ã§ããããã¹ã㪠API ã«ãªãã®ãé²ãã¤ã¤ãjs-data ã¤ã³ã¿ãã§ã¤ã¹ã®æ©æµãããããã¨ãã§ãã¾ãã
é¢é£ãªã½ã¼ã¹ã®å¤æ´ã¯èªåã§åæ ããã
æ°ããåãã¼ã¿ã®ä½æï¼åé¤ãªã©ã§ãã¼ã¿ã¹ãã¢å
ã®é¢é£ãªã½ã¼ã¹ã®å¤ãå¤æ´ãããã¨ãããã«åæ ããã¾ããä¸è¨ã®ä¾ã§ã¯ãBook ãªã½ã¼ã¹ãä½æãããã㨠Author ãªã½ã¼ã¹ããã¯åã« .books
ã¨ã¢ã¯ã»ã¹ããã ãã§æ°ãã Book ãªã½ã¼ã¹ãè¦ããããã«ãªãã¾ãã
ã¤ãã³ã
ãã¼ã¿ã®æ´æ°ãæ¤ç¥ããããã®ã¤ãã³ãéç¥ã®ä»çµã¿ãç¨æããã¦ãã¾ãããªã½ã¼ã¹ã®ã©ã¤ããµã¤ã¯ã«ããããã®ã¿ã¤ãã³ã°ã§éã£ãã¤ãã³ããéç¥ããã¾ãã
ãªã¹ãã®ç»é²ï¼è§£é¤ãªã©ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¯ EventEmitter ã¨åä¸ã§ãon
㨠off
ãemit
ãç¨æããã¦ãã¾ãã
æ°ãããã¼ã¿ããã¼ã¿ã¹ãã¢ã«æ¿å ¥ãããã¨ãã«éç¥ãåãã¦ä½ãå¦çãå®è¡ããä¾ãæ¸ãã¦ã¿ã¾ãããã
notifyNewBookTitle = function (resource, instance){ console.log("æ°ããæ¬ã追å ããã¾ãã:" + instance.title) } // DS.afterInject ã¯ãã¼ã¿ã¹ãã¢ã«æ°ããã¤ã³ã¹ã¿ã³ã¹ãå ¥ã£ãæã«å¼ã°ãã Book.on("DS.afterInject", notifyNewBookTitle); Book.create({ authorId: 1, title: "æãåºãã©ã³ã", publishedIn: "1980" }); // => 'æ°ããæ¬ã追å ããã¾ãã: "æãåºãã©ã³ã"' Book.off("DS.afterInject", notifyNewBookTitle); Book.create({ authorId: 1, title: "ãã»ãã", publishedIn: "1981" }); // => ''
ãã®ä»ä¾¿å©ãªæ©è½
ããã§ã¯ç´¹ä»ãããã¾ããã§ãããã以ä¸ã®ãããªæ©è½ãå®ç¾ããã¦ãã¾ãã
- ãªã½ã¼ã¹ã®ããªãã¼ã·ã§ã³
- Computed property
- ãã¼ã¿æ´æ°å¾ä¸åº¦ã ãå¤ãè¨ç®ãã¦ä¿æãã¦ããæ©è½
- localStorge ä¸ã«ãããã¼ã¿ãªã©ãHTTP ãã¼ã¹ã§ãªããªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹
注æ - æ¤ç´¢æ§ã®æªã
js-data ã¯ç´ æ´ãããã©ã¤ãã©ãªã§ãããå°ã ä¸è¬çãããååã®ããããæ¤ç´¢ã¨ã³ã¸ã³ã§æ å ±ãæ¢ãã®ã«è¦å´ãã¾ãã ã©ã¤ãã©ãªåã§æ¤ç´¢ãã¦ããå ¬å¼ãã¼ã¸ã® Google ã§ã®æ¤ç´¢é ä½ã¯ 4 ä½ããã®ä»ã«ã²ã£ããã£ã¦ãããã¼ã¸ãJavaScript 㧠HTML5 ã® data å±æ§ãæä½ããæ¹æ³ã®ãã¼ã¸ãå¤ãã£ããã¨ããªããªãæãããã«æ å ±ãéãããã¾ããã
åé¡ã®è§£æ±ºçãæ å ±ãã»ããæã¯ãå ¬å¼ããã¥ã¡ã³ãï¼ã¨ã¦ãããã¾ã¨ã¾ã£ã¦ãã¾ãï¼ã Github issuesãStack Overflow ãªã©ãç´æ¥æ¤ç´¢ããã¨è¯ãã¨æãã¾ããå ¬å¼ã® Slack ãã£ã³ãã«ãããã¾ãã®ã§ãããã§éçºè ã«ç´æ¥å°ãã¦ã¿ãã®ãæã§ãã
ãããã«
Redux ãªã©ã® Flux ãã¬ã¼ã ã¯ã¼ã¯ãå°å ¥ããã¾ã§ããªããã ãã©ãReact.js ã¨çµã¿åããã¦ãã¼ã¿ã®ã¢ã¯ã»ã¹ã®é¨åã ãæ½è±¡åãã¦ãããã©ã¤ãã©ãªã¯ãªãããªâ¦â¦ãã¨ãããã¨ã§èª¿æ»ããã®ããjs-data ãè¦ã¤ãããã£ããã§ããã èªå以å¤ã®ç¹å®ã®ãã¬ã¼ã ã¯ã¼ã¯ã«ä¾åãã¦ããªãã®ã§ãReact.js ã§ç®¡çãããã³ã³ãã¼ãã³ãã¨ãã以å¤ã®ã³ã³ãã¼ãã³ãã両æ¹ã§ãã®ã©ã¤ãã©ãªãã¢ãã«å±¤ã¨ãã¦ä½¿ç¨ãããã¨ãã§ãã¾ãã
ååã§æããã¦ããæããããã®ã§ããããªããªã使ãããããã¤ã§ãããã®æ©ä¼ã«ãã²è§¦ã£ã¦ã¿ã¦ãã ããã