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

Cannot Cache the data for js-data actions #366

Open
VenkatRamReddyK opened this issue Oct 7, 2016 · 2 comments
Open

Cannot Cache the data for js-data actions #366

VenkatRamReddyK opened this issue Oct 7, 2016 · 2 comments
Labels

Comments

@VenkatRamReddyK
Copy link

Api Endpoint: : /test/users/:userId/settings

I need to use Caching with js-data actions.

$provide.factory('UserSettings', function (DS, $q, $log) {

      function handleResponse( response ) {
        var Resource = DS.definitions.userSettings;
        response = Resource.deserialize( Resource, response );
        return response;
      };

      var _userSettings = DS.defineResource({
        basePath: '/test',
        endpoint: '/users',
        name: 'userSettings',
        cacheResponse: true,
        bypassCache: false,
        actions: {
          getSettings: {
            method: 'GET',
            pathname: 'settings',
            response: handleResponse
          },
          saveSettings: {
            method: 'PUT',
            pathname: 'settings',
            response: handleResponse
          }
        },
        deserialize: function ( resourceConfig, data ) {
          if ( data.config.method === 'PUT' || data.config.method === 'GET' ) {
            return data.data;
          }
          else if ( data.config.method === 'DELETE' || data.config.method === 'POST' ) {

          }
        }
      });
      return _userSettings;
    });

@jmdobry
Copy link
Member

jmdobry commented Oct 7, 2016

You have to do it yourself, something like this:

function handleResponse( response ) {
  var Resource = DS.definitions.userSettings;
  var data = Resource.deserialize( Resource, response );

  // Inject the data into the store and return it
  return Resource.inject(data);
};

@VenkatRamReddyK
Copy link
Author

Error:
index.js:107 userSettings Error: userSettings.inject: "attrs" must contain the property specified by "idAttribute"! null

Work around: passing in id to inject
But Wondering how do I retrieve the id property so that I can inject it in the following way
Should I use computed attribute ?.

function handleResponse( response ) {
        var Resource = DS.definitions.userSettings;
        response = Resource.deserialize( Resource, response );
        return Resource.inject(response,id);
      };

How do I get the userId for the computed attribute?

DS.defineResource({ 
 ,...
 computed: {
          id: function (userId) {
            return userId;
          }
        },

Caller: @params userId

UserSettings.getSettings( userId )
        .then( function ( userSettingsResponse ) {
         },...

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

No branches or pull requests

2 participants