These are chat archives for js-data/js-data

24th
May 2015
Critical-Impact
@Critical-Impact
May 24 2015 06:10
Does anyone know if there is a way to dynamically alter the basePath or endpoint after the resouce has been defined? Basically I'm using localforage and I want to have multiple users stored in the same database. I can set the basePath or endpoint to the user's ID for example but once it's set I cant seem to change it. I am aware I can pass in options for each call to findAll, create, etc but I feel like it'd be cleaner if I could change the basePath or endpoint on that resource globally
Marian Ondrasak
@ondrasak
May 24 2015 18:26
Is there way how to update model in browser by data that comes from stream (sockets) ? .. like method 'refresh' that can get new data as param (I want to avoid new http request)?
Jason Dobry
@jmdobry
May 24 2015 20:49
@Critical-Impact Why are you trying to change the basePath? Why would you set it to a user's ID? Are you trying to store data of different users who are using the same browser? If you've defined a User resource, any item in the user store will have its own unique path.
@ondrasak Yes, here's some example code from one of my apps (js-data + js-data-angular + angular):
var socket = io.connect(app.url, { path: '/api/socket.io' });

  socket.on('create', function (data) {
    if (data.ownerId && $rootScope.loggedInUser && $rootScope.loggedInUser.id === data.ownerId) {
      DS.find(data.resource, data.id);
    }
    $rootScope.$broadcast('create', data.resource, data.id, data.ownerId);
  });
  socket.on('update', function (data) {
    if (data.id === 'all' && data.seriesId) {
      angular.forEach(DS.filter(data.resource, { seriesId: data.seriesId }), function (instance) {
        DS.refresh(data.resource, instance.id);
      });
    } else {
      DS.refresh(data.resource, data.id);
    }
    $rootScope.$broadcast('update', data.resource, data.id, data.ownerId);
  });
  socket.on('destroy', function (data) {
    if (data.id === 'all' && data.lessonId) {
      DS.ejectAll(data.resource, { lessonId: data.lessonId });
    } else {
      DS.eject(data.resource, data.id);
    }
    $rootScope.$broadcast('destroy', data.resource, data.id, data.ownerId);
  });
@ondrasak If you send the entire updated item into the socket, then you can just use DS#inject
Marian Ondrasak
@ondrasak
May 24 2015 22:17
@jmdobry thank you! That was a very helpful response.