Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpack function unable to determine name on nested resources #224

Closed
garethhudson07 opened this issue Dec 23, 2014 · 4 comments
Closed

unpack function unable to determine name on nested resources #224

garethhudson07 opened this issue Dec 23, 2014 · 4 comments
Labels
Milestone

Comments

@garethhudson07
Copy link

Hi guys

Just started using the library and really liking it so far.

I am having an issue with nested resources and getting the following error:

Collection $decode expected array

var app = angular.module('app', ['restmod']);

app.controller('Ctrl', function($scope, Product) {
    var product = Product.$find(105);
    product.items.$fetch();
});

app.config(function(restmodProvider) {
    restmodProvider.rebase('AMSApi');
});

app.factory('Product', function(restmod) {
    return restmod.model('http://api.devname.com/v1/shops/testshop/products').mix({
        items: { hasMany: 'Item' }
    });
});

app.factory('Item', function(restmod) {
    return restmod.model();
});

My json responses are as follows:

{"product":{"id":105,"name":"product name","description":"product descripion","createdAt":"2014-12-18 13:44:47","updatedAt":"2014-12-20 17:52:50","metadata":{"title":"title","description":"description","keywords":"one,two,three"}}}
{"items":[{"id":406,"price":"10.00","createdAt":"2014-12-18 13:44:59","updatedAt":"2014-12-20 17:40:00"}]}

I have left the url blank in the nested resource as per my understanding from the docs. However it seems that the unpack function is unable to determine the name.

when I added a console.log(name) to the unpack function it ouput undefined

return restmod.mixin(function() {
    this.define('Model.unpack', function(_resource, _raw) {
      var name = null,
          links = this.getProperty('jsonLinks', 'linked'),
          meta = this.getProperty('jsonMeta', 'meta');

      if(_resource.$isCollection) {
        name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');

        //name = 'items';

        console.log(name);  //ouputs undefined

      } else {
        // TODO: use plural for single resource option.
        name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
      }

      if(meta) {
        _resource.$metadata = {};
        processFeature(_raw, name, meta, links, function(_key, _value) {
          _resource.$metadata[_key] = _value;
        });
      }

      if(links) {
        processFeature(_raw, name, links, meta, function(_key, _value) {
          // TODO: check that cache is an array.
          packerCache.feed(_key, _value);
        });
      }

      return _raw[name];
    });
  });

Is this a bug? If not any guidance on what I am doing wrong would be greatly appreciated :)

@iobaixas
Copy link
Member

iobaixas commented Jan 7, 2015

Hi @garethhudson07, when using nested resources you should explicitly provide a name for the resource, like this:

app.factory('Item', function(restmod) {
  return restmod.model().mix({
    $config: { name: 'item' }  
  });
});

That way the unpacker knows where to look for data in the response.

@iobaixas iobaixas closed this as completed Jan 7, 2015
@garethhudson07
Copy link
Author

Hi @iobaixas, thanks for your reply.

I have updated my code but am still having problems and am getting the same error as before Collection $decode expected array

It seems that the getProperty('plural') method is returning undefined despite the fact that getProperty('name') now returns "item";

return restmod.mixin(function() {
    this.define('Model.unpack', function(_resource, _raw) {
      var name = null,
          links = this.getProperty('jsonLinks', 'linked'),
          meta = this.getProperty('jsonMeta', 'meta');

      if(_resource.$isCollection) {
        name = this.getProperty('jsonRootMany') || this.getProperty('jsonRoot') || this.getProperty('plural');

        console.log(this.getProperty('name')); //returns item
        console.log(this.getProperty('plural')); //returns undefined

      } else {
        // TODO: use plural for single resource option.
        name = this.getProperty('jsonRootSingle') || this.getProperty('jsonRoot') || this.getProperty('name');
      }

      if(meta) {
        _resource.$metadata = {};
        processFeature(_raw, name, meta, links, function(_key, _value) {
          _resource.$metadata[_key] = _value;
        });
      }

      if(links) {
        processFeature(_raw, name, links, meta, function(_key, _value) {
          // TODO: check that cache is an array.
          packerCache.feed(_key, _value);
        });
      }

      return _raw[name];
    });
  });

I would really appreciate any guidance on this.

Thanks :)

@iobaixas
Copy link
Member

iobaixas commented Jan 7, 2015

I was wrong 😞, the plural property does not get generated automatically if you change the name property, you will have to set the plural name too in the $config block:

app.factory('Item', function(restmod) {
  return restmod.model().mix({
    $config: { 
      name: 'item', 
      plural: 'items' 
    }  
  });
});

I'll mark this as a bug, the fix is not easy though...

@iobaixas iobaixas reopened this Jan 7, 2015
@iobaixas iobaixas added bug and removed bug labels Jan 7, 2015
@iobaixas iobaixas added this to the 1.2 milestone Jan 7, 2015
@garethhudson07
Copy link
Author

Hi @iobaixas, that's got it working.

Thanks for your help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants