strongloop-community / loopback-sdk-ember

Ember.js SDK for LoopBack
Other
6 stars 3 forks source link

loopback sdk generate for ember-data #1

Open tantou opened 10 years ago

tantou commented 10 years ago

https://github.com/enshjiang/loopback-ember-generate

Is this thinking right ?

mgenev commented 10 years ago

can you please write a quick summary of your approach?

tantou commented 10 years ago

sure loopback has models and relationships Ember-Data also has models and relationships So,I export the loopback models information to the client-side (Ember-Data Model) for example:

//loopback model
{
  "name": "Author",
  "plural": "Authors",
  "base": "PersistedModel",
  "properties": {
    "username": {
      "type": "string",
      "required": true
    },
    "password": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "posts": {
      "type": "hasMany",
      "model": "Post",
      "foreignKey": "userId",
      "options":{
        "async":false
      }
    }
  },
  "acls": [],
  "methods": []
}
//export to ember-data model 
DS.Model.extend({
  //attributes
  // relationships
  // validations
  // interface (reserved)
});

1.loopback has provide many interfaces to CRUD a model, and Ember-Data also have provide CRUD ,but have not implement :isExists,count,findOne,update 2.So I implement the unimplement method on store && adapter

Question

1.how to use the api like posts/{id}/comments/{fk} in Ember-Data 2.the loopback validations is still have bug

mgenev commented 10 years ago

I was going after the same thing plus integrating this model json file with ember cli's generator through a blueprint, but then I was told by the loopback guys that I need the runtime meta data, as the json data is incomplete: https://groups.google.com/forum/#!searchin/loopbackjs/martin$20genev/loopbackjs/jM3LkR-_Q4c/ZEEunjM3N2wJ

now I'm in a bit of a limbo as I haven't studied the angular example and the meta data yet

tantou commented 10 years ago

I also use the runtime meta data ,like the loopback-sdk-angular, Do you know how to use the api posts/{id}/comments/{fk} in Ember-Data ? I still have trouble in it

mgenev commented 10 years ago

can you point me to where you make use of the runtime data in your code? how do you export it etc?

tantou commented 10 years ago

I have implement the corresponding serializer in loopback to serialize and extract the data ember-data send to backend

tantou commented 10 years ago

sure

lib/service.js  start the loopback app and return model meta data
lib/generate.js use the model meta data and then render the templates/model.js 
mgenev commented 10 years ago

about requesting posts/{id}/comments/{fk}, I would not make the request with ember data, I would do a normal ajax call and then side load the payload see this: http://emberjs.com/api/data/classes/DS.Store.html

tantou commented 10 years ago

Can you show an example like if I want to use the api posts/{id}/comments/{fk}, My idea:

App.Comment = DS.Model.extend({
   post : DS.belongsTo('post')
});
App.Post = DS.Model.extend({
   comments : DS.hasMany('comment')

    //here is my implement
    get : function(keyName,tolerant){
      if(!tolerant){
           this._super(keyName);
       }else{
           //calculate the relationship between two model 
           // keyName:'comments', post has many comments
           // url :/posts/{id}/comments/{fk}
          //use adapter to request 
       }
    }
});
tantou commented 10 years ago

@mgenev I have the idea how to use the posts/{id}/comments/{fk} posts/{id}/comments/{fk} is like /comments?filter[where][property]=value So I don't need to use the api post/{id}/comments/{fk}, I just use the /comments && filter ,so I can get the same effect

medokin commented 9 years ago

Have you already integrated hasMany relations for loopback in Ember? If so, how exactly?

mattwilliams commented 9 years ago

Hi I have been looking at this too. It seems ember data expects the server to provide an additional attribute on the declaring hasMany model, which provides the ids of the related models. Then ember data will make subsequent calls to get the related models. The subsequent calls pass the related model ids to the server on the query string.

leifdejong commented 8 years ago

@mattwilliams managed to do this in loopback using the "references" relationship Embedded Relationships but what worries me is that ember makes multiple requests for each relationship ID. Any solution to this?

waleedq commented 8 years ago

Hi i have been struggling with ember-data and nested api urls provided by loopback as ember-data doesn't support nested urls.

I've found that ember-data adapter has a findHasMany method which would load hasMany from the property "links" if it's returned in the response payload.

example user model

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  password: DS.attr('string'),
  accounts: DS.hasMany('account')
});

example user payload

{
 "name" : "lee",
 "email" : "lee@foo.pizza",
 "links" : {"accounts": "/User/1/accounts"}
}

Once the user.accounts is accessed the adapted would load the user accounts hasMany relationship using the "accounts" link provided in the payload.

To add the links property you can implement that in the loopback backend using model hooks i guess, or you can define a custom serializer for the user model in your ember application which overrides the normalizeResponse method to add the links property to the payload

This is my custom serializer:

defined in /myAppPath/serializers/user.js

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  normalizeResponse(store, primaryModelClass, payload, id, requestType) {
    // check if links is already defined in the response payload
    payload.links = payload.links || {}
    // define the accounts url if it's already defined 
    payload.links.websites = payload.links.accounts || `/User/${id}/accounts`
    // pass the new payload and return;
    return this._super(...arguments);
  },
});

As stated in the adapter findHasMany method documentation urls can be defined in different formats as follows:

The format of your links value will influence the final request URL via the urlPrefix method: Links beginning with //, http://, https://, will be used as is, with no further manipulation. Links beginning with a single / will have the current adapter's host value prepended to it. Links with no beginning / will have a parentURL prepended to it, via the current adapter's buildURL.

I am using ember v2.4.3, with Ember Data v2.4.3

mattwilliams commented 8 years ago

How is this coming along? The above works but will make an additional ajax request for each belongsTo relationship in an entire collection. I am trying to bootstrap a json object that has embedded relationships. However there is a specific structure that Ember expects. Which means having to write custom model hooks in loopback. Not impossible but looking for something a little bit easier.