Help us understand the problem. What is going on with this article?

AlloyのModelでREST APIからデータを取得する

More than 5 years have passed since last update.

Githubで公開されていたREST API用のAdapterを利用してREST APIからデータを取得します。

実装方法

Adapterを導入

PROJECT_DIRECTORY/app/asset/alloy/syncにrestapi.jsを配置する。

ModelにAdapterを設定する

Modelで導入したAdapterを使用するように設定します。adapterとURLのあたり。
jsを使っている方は読み替えてください。

user.coffee
exports.definition =
  config:
    columns:
      id: 'string'
      name: 'string'
    URL: 'http://localhost:8080/user'
    adapter:
      type: 'restapi'
      collection_name: 'user'
      idAttribute: 'id'
    extendModel: (Model) ->
      _.extends Model.prototype, () ->
      Model
    extendCollection: (Collection) ->
      _.extends Collection.prototype, () ->
      Collection

REST APIからCollectionを生成

適当なControllerでREST APIからデータを取得します。

hoge.coffee
users = Alloy.createCollection 'user'

users.fetch
  success: () ->
    users.forEach (user) ->
      Ti.API.info user.id
  error: () ->
    Ti.API.info 'error'

困ったこと

ModelのなかでAlloy.CFGからリソースのurlを取ろうとしたんだけど取得できず。Alloy = requre 'Alloy'したらいける。
ModelではグローバルにAlloyが注入されてないんですね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした