ã¢ãã«ã¯ã¢ããªã±ãŒã·ã§ã³ã®éåžžã«éèŠãªéšåã§ããããŒã¿ããªããã°ã©ãã«ãååšããªãããã§ãã
ãã®èšäºã§ã¯ãã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³Appcelerator Titaniumã®éçºã®ããã®MVCãã¬ãŒã ã¯ãŒã¯ã§ã®ã¢ãã«ã®äœ¿çšã®ããããåŽé¢ãå¯èœãªéã詳现ã«ã«ããŒããããã«ããŸãã
ã¢ãã«ãžã®é£çµ¡ãè©Šã¿ãŠããªãå Žåã¯ããã®èšäºãæ°ããã®ç¥çµãç¯çŽããããšãé¡ã£ãŠããŸãã
Backbone.js
ãããã£ãŠãTitaniumã§ã¢ãã«ãæäœãããšãã«æåã«ç¥ã£ãŠããã¹ãããšã¯ãããããBackboneã®ã¢ãã«ãšã³ã¬ã¯ã·ã§ã³ã«åºã¥ããŠãããšããããšã§ãã ããã¯ããŸããBackboneã®ããã¥ã¡ã³ãã§èª¬æãããŠãããã¹ãŠã®ããããã£ãšã¡ãœããã䜿çšã§ããããšãæå³ããŸãã 第äºã«ãããªãããŸã ããããŠããªããã°ãæåã«åœŒå¥³ãç¥ãããšã¯äžå¿ èŠã§ã¯ãããŸããã
念ã®ãããçšèªã«çŠç¹ãåœãŠãŸããã¢ãã«ã¯åäžã®ãšã³ãã£ãã£ã§ãããã³ã¬ã¯ã·ã§ã³ã¯é¡äŒŒããã¢ãã«ã®ã»ããã§ãã
éèŠïŒTitaniumã¯BackboneããŒãžã§ã³0.9.2ã䜿çšãããããBackboneããã¥ã¡ã³ãã®äžéšã®ã¡ãœãããå®è£ ãããªãå ŽåããããŸãã
Underscore.js
ã³ã¬ã¯ã·ã§ã³ã¯Underscore.jsã¡ãœãããç¶æ¿ããéçºè ã®äœæ¥ã楜ã«ããŸãã ããšãã°ã .mapã¡ãœãã
äœæ
2ã€ã®åçã®æ¹æ³ã§ã¢ãã«ãäœæã§ããŸããAppceleratorStudioã䜿çšããæ¹æ³ãšæåã§è¡ãæ¹æ³ã§ãã
<project> / app / models /ãã©ã«ããŒã«ç¹å¥ãªåœ¢åŒã®ãã¡ã€ã«ãæåã§äœæã§ããŸãïŒå®éã«ã¯ãã¹ã¿ãžãªã«ãã£ãŠèªåçã«äœæãããŸãïŒã
ã¢ãã«èšè¿°ãã¡ã€ã«åœ¢åŒ
exports.definition = { config : { // }, extendModel: function(Model) { _.extend(Model.prototype, { // Backbone.Model }); return Model; }, extendCollection: function(Collection) { _.extend(Collection.prototype, { // Backbone.Collection }); return Collection; } }
ã¹ã¿ãžãªã®ãã«ãã䜿çšããå Žåã¯ããããžã§ã¯ãã®ã³ã³ããã¹ãã¡ãã¥ãŒãåŒã³åºããŠããæ°èŠ->åéã¢ãã«ãã®é ç®ãéžæããã ãã§ãã ãã€ã¢ãã°ããã¯ã¹ãäœæãããšããã¢ããã¿ã®ã¿ã€ããéžæããããã«æ±ããããŸãïŒlocalStorageãpropertiesããŸãã¯sqlã ã¢ããã¿ãŒã®ã¿ã€ãã«ã€ããŠä»¥äžã«èª¬æããŸãã
åºæ¬çãªæ©èœã®ããã«ãã®ãã¡ã€ã«ã«èšè¿°ããå¿ èŠãããã®ã¯ãæ§æãããã¯ã ãã§ãã ããŒã¿ã¹ããŒããšåæã¢ããã¿ãŒã®ã¿ã€ããå«ãŸããŠããŸãã ãã®ãããã¯ã«ã¯ã次ã®ãã£ãŒã«ããå«ãŸããŠããå¿ èŠããããŸãã
- columns ïŒã¢ãã«ã®ãã£ãŒã«ãã説æããèŸæžã ããŒã¯ãã£ãŒã«ãã®ååãå€ã¯ã¿ã€ãã§ãã SQLiteã®ãããªåïŒ æååãvarcharãintãtinyintãsmallintãbigintãdoubleãfloatãdecimalãnumberãdateãdatetimeãboolean
- ããã©ã«ã ïŒåãšåããã£ã¯ã·ã§ããªããã ããåæäžã«å®çŸ©ãããŠããªãå Žåãå€ã¯ããã©ã«ãã®ãã£ãŒã«ãå€
- adapter ïŒãã®ãªããžã§ã¯ãã«ã¯2ã€ã®ãã£ãŒã«ããå«ãŸããŸãã
- type ïŒåæã¢ããã¿ãŒã®ã¿ã€ã
- collection_name ïŒ ã³ã¬ã¯ã·ã§ã³ã®ååã ã¢ãã«åãšç°ãªãå ŽåããããŸãã ãã®ãã£ãŒã«ãã®å€ã䜿çšããŠãã³ã¬ã¯ã·ã§ã³ãäœæããŸãã
- idAttribute ïŒäž»ããŒ
ããšãã°ãæžç±ã¢ãã«ãäœæããŸãã ãããè¡ãã«ã¯ã<project> / app / models /ãã©ã«ããŒã«books.jsãã¡ã€ã«ãäœæããŸãã
äœæãããã¢ãã«ã®äŸ
exports.definition = { config: { "columns": { "title": "String", "author": "String" // sql, SQLite keywords }, "defaults": { "title": "-", "author": "-" }, "adapter": { "type": "sql", "collection_name": "books", "idAttribute" : "title", "db_file" : "db_books", // , . _alloy_ "db_name" : "books.sqlite", // /app/assets. , [_].sqlite } } }
ããã«ã¯ãæ¬ã®ååãšèè ã®2ã€ã®ããã¹ããã£ãŒã«ããæã€ã¢ãã«ããããŸãã äž¡æ¹ã®ãã£ãŒã«ãã®ããã©ã«ãã¯ã-ãã§ãã ãããŸã§ã®ãšããããã¹ãŠãã·ã³ãã«ã§ãã
sqlã¢ããã¿ãŒã¿ã€ãã¯ãããŒã¿ãååŸããããšãããšãã«ãçµã¿èŸŒã¿ã¢ããã¿ãŒã䜿çšããã¢ãã«ãããã€ã¹äžã®SQLiteããŒã¿ããŒã¹ã«æ¥ç¶ããbooksããŒãã«ããããŒã¿ãååŸããããšããããšã瀺ããŸãã
ã¢ãã«å ã®ç¬èªã®ããããã£ãšã¡ãœããã¯ã次ã®ããã«è¿œå ã§ããŸãã
ã¢ãã«æ¡åŒµã®äŸ
exports.definition = { config : { // }, extendModel: function(Model) { _.extend(Model.prototype, { // Backbone.Model customProperty: 'book', customFunction: function() { Ti.API.info(', '); }, }); return Model; } }
ã³ã¬ã¯ã·ã§ã³ã«ãåãããšãèšããŸãã extendModelã«ç¬èªã®ã¡ãœãããè¿œå ããå Žåãã¢ãã«ã§äœ¿çšã§ããŸãããã³ã¬ã¯ã·ã§ã³ã¯äœ¿çšã§ããŸãããéã®å Žåãåæ§ã§ãã
䜿çšãã
ã¢ãã«/ã³ã¬ã¯ã·ã§ã³ã«ã¢ã¯ã»ã¹ããã«ã¯ã2ã€ã®æ¹æ³ããããŸãã
- ã°ããŒãã«ã·ã³ã°ã«ãã³ã®äœæ
- ããŒã«ã«ãªã³ã¯ãäœæãã
- Alloyã䜿çšããŠããå ŽåãXMLããŒã¯ã¢ããã§æå®ããããšã«ãã
ã°ããŒãã«ã·ã³ã°ã«ãã³
ãã®å Žåãã¢ãã«ã¯ã©ãã§ãããã¹ãŠã®ã³ã³ãããŒã©ãŒã§ããã«äœ¿çšã§ããŸãã ãã§ã«å®£èšãããŠããå Žåã¯ãå¥ã®å®£èšã§Alloy.Models.instanceã¡ãœãããåŒã³åºããšããã®å®£èšãããã¢ãã«ãžã®ãªã³ã¯ãè¿ãããŸãã äŸã¯ãŠãŒã¶ãŒã¢ãã«ã§ã;ããã¯ã©ãã§ãåç¬ã§ããå ŽåããããŸãã
ã³ã¬ã¯ã·ã§ã³ã«ã€ããŠãåæ§ã§ãã
ããŒã«ã«ãªã³ã¯
Alloy.createModelã¡ãœããã䜿çšããŠãããŒã«ã«ãªã³ã¯ãäœæã§ããŸãã ãã®ã¢ãã«ã¯ãäœæãããã³ã³ãããŒã©ãŒå ã§ã®ã¿äœ¿çšå¯èœã«ãªããŸãã ã¡ãœããã¯ãã¡ã¯ããªãŒã§ãããã¢ãã«åïŒãã¡ã€ã«åãã.jsãåŒãããã®ïŒãšãã©ã¡ãŒã¿ãŒãæž¡ããŸãã ãããŠããã ãã§ãã
ã³ã¬ã¯ã·ã§ã³ã«ã€ããŠãåæ§ã§ãã
XML
<Model>ããŒã¯ã¢ããèŠçŽ ã¯ã <Alloy>ã®çŽæ¥ã®åå«ã§ãªããã°ãªããŸããã ãŸããå€ããmodel-file-name-minus-.jsãã®srcå±æ§ãå¿ èŠã§ãã 次ã«ãAlloy.Modelsåå空éã®ãã®ã¢ãã«ã®ã·ã³ã°ã«ãã³ãã³ã³ãããŒã©ãŒã§äœ¿çšå¯èœã«ãªããŸãã
XML
<Alloy> <Model src="book" /> <Window> <TableView id="table" /> </Window> </Alloy>
ã³ã³ãããŒã©ãŒ
var drama = Alloy.Models.book; drama.set('title', ' '); drama.set('author', ' ');
ãã®èŠçŽ ã«ã¯ã€ã³ã¹ã¿ã³ã¹å±æ§ããããŸãã trueã®å Žåãã¢ãã«ãžã®ãªã³ã¯ãäœæãããŸãïŒããŒã«ã«ã1ã€ã®ã³ã³ãããŒã©ãŒå ã§ã®ã¿äœ¿çšå¯èœïŒã ãšããã§ããã®å Žåãã¢ãã«ã¯Alloy.Modelsåå空éã§ã¯ã¢ã¯ã»ã¹ã§ããŸãããIDã§ã¢ã¯ã»ã¹ããå¿ èŠããããŸãã
XML
<Alloy> <Model id="myBook" src="book" instance="true"/> <Window> <TableView id="table" /> </Window> </Alloy>
ã³ã³ãããŒã©ãŒ
var drama = $.myBook; drama.set('title', ' '); drama.set('author', ' ');
ã³ã¬ã¯ã·ã§ã³ã«ã€ããŠãåæ§ã§ãã
åæã¢ããã¿ãŒ
æåŸã«ããäœæãã»ã¯ã·ã§ã³ã§çŽæãããã»ã¯ã·ã§ã³ã«è¡ããŸããã ã¢ãã«ãæäœããã«ã¯ãããŒã¿ã¹ããŒããèšè¿°ããã ãã§ã¯äžååã§ãã ãŸããããããäœãã«æ¥ç¶ããå¿ èŠããããŸãã ãã®æ¥ç¶ã¯åæã¢ããã¿ãŒã§ãã
ã¢ããã¿ã¯ãBackbone.syncã®å®è£ ã§ãã æãäžè¬çãªã¿ã¹ã¯ã¯ãªã¢ãŒããµãŒããŒã§ã®CRUDæäœã§ããããïŒå人çã«ã¯ç§ã®çµéšããïŒãããã©ã«ãã§ã¯ãBackbone.syncã¡ãœããã¯ãModel.urlRootããã³Collection.urlããããã£ã§æå®ããã¢ãã¬ã¹ã«å¯ŸããŠRESTful JSONãªã¯ãšã¹ããè¡ããŸãã ããã¯å ¬åŒããã¥ã¡ã³ãã§ãã å®éã«ã¯ãã¢ãã«/ã³ã¬ã¯ã·ã§ã³ã®æèŠãã€ããããã«ãrestapiã¢ããã¿ãŒã䜿çšãããšéåžžã«äŸ¿å©ã§ããããã«ã€ããŠã¯åŸã§èª¬æããŸãã ãããŸã§ã®éãå ¬åŒããã¥ã¡ã³ãã«æ»ããŸãã
ã¢ããã¿ã«ã¯æ¬¡ã®4ã€ã®ã¿ã€ãããããŸãã
- sql
- ç©æ§
- localStorageïŒåé1.5.0以éã§ã¯äœ¿çšãããªããããèæ ®ããŸããïŒ
- ããªãã®ã¿ã€ã
SQL
idAttributeããŒãadapterã»ã¯ã·ã§ã³ã®configã»ã¯ã·ã§ã³ã§å®çŸ©ãããŠããªãå ŽåãAlloyã¯ããŒãã«ã«Alloy_idãã£ãŒã«ããçæãããããäž»ããŒãšããŠäœ¿çšããŸãã
db_fileããã³db_nameæ§æãã£ãŒã«ãïŒãäœæãã»ã¯ã·ã§ã³ïŒã«æ³šæããŠãã ããã ããã§éèŠã§ãã
ä»ã®ã¿ã€ããšã¯ç°ãªãããã®ã¡ãœããã§ã¯ã Backbone.Collection.fetchã¯1è¡ã®SQLã¯ãšãªãååŸã§ããŸãã
.fetchïŒïŒã®ã¯ãšãªæåå
var library = Alloy.createCollection('book'); // - collection_name var table = library.config.adapter.collection_name; // library.fetch({query:'SELECT * from ' + table + ' where author="' + searchAuthor + '"'}); // prepared statement library.fetch({query: { statement: 'SELECT * from ' + table + ' where author = ?', params: [searchAuthor] }});
ã€ãŸããåçŽãªãªã¯ãšã¹ããå¿ èŠãªå Žåã¯ããã©ã¡ãŒã¿ã«ã¯ãšãªããŒãæå®ããŠãªããžã§ã¯ããæž¡ããè€éãªãªã¯ãšã¹ããå¿ èŠãªå Žåã¯ããªã¯ãšã¹ããšãã©ã¡ãŒã¿ã®äž¡æ¹ãæž¡ããŸãã
ãšããã§ããŸã idããŒãæž¡ãããšãã§ããŸãã ãã®å Žåãã¢ããã¿ãŒèªäœã¯WHERE id =ïŒ..å¶çŽã䜿çšããããšãç解ããŸãã
id
myModel.fetch({id: 123}); // , myModel.fetch({query: 'select * from ... where id = ' + 123 });
ç©æ§
ããŒã¿ããŒã¹ãSQLiteã§ãªãå Žåãç¬èªã®ã¢ããã¿ãŒãäœæããããŒã¿ã䜿çšããŠäœã§ãã§ããŸãã ãããè¡ãã«ã¯ãapp / assets / Alloy / syncãŸãã¯app / lib / Alloy / syncãã£ã¬ã¯ããªã«ã¢ããã¿ãŒãã¡ã€ã«ïŒmyAdapter.jsãªã©ïŒãäœæããadapter.typeã®ã¢ãã«æ§æã§ãã®ãã¡ã€ã«ã®ååãã€ãŸããmyAdapterããæå®ããŸãã
å®éããã®ãã¡ã€ã«ã¯3ã€ã®é¢æ°ããšã¯ã¹ããŒãããå¿ èŠããããŸãã
- module.exports.beforeModelCreateïŒconfigïŒ ïŒãªãã·ã§ã³ïŒ-ååã瀺ããšãããã¡ãœããã¯ã¢ãã«ãäœæããåã«å®è¡ãããŸãã ãã©ã¡ãŒã¿ãŒã¯ããã®ã¢ãã«ã®æ§æãæž¡ããŸããã ããã§ã¯ãããšãã°ããªã¯ãšã¹ãã¢ãã¬ã¹ã«baseUrlãè¿œå ã§ããŸãã å€æŽãããæ§æãªããžã§ã¯ããè¿ããŸã
- module.exports.afterModelCreate ïŒãªãã·ã§ã³ïŒ -2ã€ã®ãã©ã¡ãŒã¿ãŒãåãå ¥ããŸãïŒBackbone.Modelãäœæããã°ããã®æ°ããã¯ã©ã¹ãšã¢ãã«ãã¡ã€ã«ã®åå
- module.exports.sync -Backbone.syncã¡ãœããã®å®è£ ã ããŒã¿ãè¿ãå¿ èŠããããŸã
ã¬ã¹ã¿ã
ãªã¢ãŒããµãŒããŒããããŒã¿ãåä¿¡ããå¿ èŠããããç¬èªã®ã¢ããã¿ãŒãäœæããããªãå Žåã¯ãçµã¿èŸŒã¿ã®ã¢ããã¿ãŒã®æ°ã«ã¯å«ãŸããŠããªããããã®ä»äºïŒRESTful JSONãªã¯ãšã¹ãïŒãå®è¡ããçŽ æŽãããrestapiã¢ããã¿ãŒããããŸãã
æäœéå¿ èŠãªèšå®-æ§æã®URLãã£ãŒã«ãïŒ
Restapiã¢ããã¿ãŒã䜿çšããã¢ãã«ã®äŸ
exports.definition = { config: { "URL": "http://example.com/api/modelname", //"debug": 1, "adapter": { "type": "restapi", "collection_name": "MyCollection", "idAttribute": "id" } }, extendModel: function(Model) { _.extend(Model.prototype, {}); return Model; }, extendCollection: function(Collection) { _.extend(Collection.prototype, {}); return Collection; } }
ãã¡ããããããã¯ãã¹ãŠå©çšå¯èœãªèšå®ã§ã¯ãããŸãã;å®å šãªãªã¹ãã«ã€ããŠã¯ãããã¥ã¡ã³ããåç §ããŠãã ããã ãã®ã¢ããã¿ãŒã§ä»ã«éèŠãªããšã¯ããã®ãã§ããã¡ãœããã3ã€ã®å¯èœãªãã£ãŒã«ããæã€èŸæžãåãå ¥ããããšã§ãã
- ããŒã¿èŠæ±ãã©ã¡ãŒã¿ãŒ
- æåãããªã¯ãšã¹ãã®æåã³ãŒã«ããã¯
- ãšã©ãŒã®ãšã©ãŒã³ãŒã«ããã¯
ãããã«
ãããã£ãŠãããã¯åºæ¬ã¬ãã«ã§Titaniumã®ã¢ãã«ãæäœã§ããããã«ããããã«ç¥ã£ãŠããå¿ èŠããããã¹ãŠã§ãã ãã®ãããã¯ã¯ããåºç¯ã§ããããšãã°ãããŒã¿ããŒã¹ã®ç°ãªãããŒãžã§ã³éã®ç§»è¡ããã»ã¹ãããŒã¿ãã€ã³ãã£ã³ã°ãAlloyã䜿çšããªãã¢ãã«ã®äœ¿çšãSQLiteã§ã®äœæ¥ã®èª¬æãããã§ã¯æå³çã«çç¥ããŸããã 次ã®èšäºã§ããããã¹ãŠã®ã®ã£ãããåããããšæã£ãŠããŸãããæåŸ ãã ããã
ãã®èšäºã¯ãaïŒå ¬åŒææžã«åºã¥ããŠããŸãã bïŒAlloyã§ã¢ãã«ã䜿çšããçµéšã habrasocietyã®äžã«ãç§ããããã®åé¡ãããç解ããŠããŠãééããäžæ£ç¢ºãªãã®ãèŠãŠãã人ããããªããã³ã¡ã³ãããŠè°è«ããŠãã ããã