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

20th
Jun 2015
Pascal DeMilly
@pdemilly
Jun 20 2015 03:29
I really love js-data and have ported my platform to it but I am still missing one important piece which is realtime binding. I know I can do it with firebase directly or even better with angularfire (love the simplicity of $firebaseArray and $firebaseObject). How can I make js-data be my in-between so I have one common interface to my backend? Any helps, pointers, gits appreciated. And thanks for an awesome lib.
Jason Dobry
@jmdobry
Jun 20 2015 04:14
@pdemilly With js-data 1.x, I can get you most of the way there. Open these two examples in different windows, and observe how they sync: https://js-data-firebase.firebaseapp.com/angular/ and https://js-data-firebase.firebaseapp.com/react/
js-data 2.x will make auto-syncing TO Firebase much easier, but with 1.x you still need to do User.save(user.id), etc.
Pascal DeMilly
@pdemilly
Jun 20 2015 04:29
This message was deleted
Wills Bithrey
@WillsB3
Jun 20 2015 11:58
@jmdobry FYI mzabriskie/axios#56 :)
Pascal DeMilly
@pdemilly
Jun 20 2015 15:30
Thank you @jmdobry No problem using save and update. I actually prefer that. If I understand correctly the code you posted you are listening to changes on the endpoints of all resources. How would you go about listening to changes on only what you explicitly bind to your scope. Also I don't use $scope anymore but controllerAs. Is bind and bindAll compatible with a VM defined by controllerAs?
Also @jmdobry what are your plans on auto-syncing for v2 and finally do you have an ETA for v2? Can we start using it for simple things?
Marian Ondrasak
@ondrasak
Jun 20 2015 15:46

hello, I have list of items like that one in example below
each item has many sections

How can aj get all items that belongs to the section with id X ?

  {
    "item_id": 1,
    "name": "Name",
    "date_created": "2015-06-15T08:37:58.000Z",
    "date_updated": null,
    "sections": [
      {
        "section_id": 18135,
        "name": "section"
      }
    ]
  }

Thanks in advance for any help

Jason Dobry
@jmdobry
Jun 20 2015 16:52
@pdemilly Yes, the example I showed you is very generic, and syncs all of the resources. If you only want to sync an individual item, you could do this:
angular.module('firebase-example', ['js-data', 'ngRoute'])
  .config(function (DSFirebaseAdapterProvider, $routeProvider) {
    var basePath = 'https://js-data-firebase.firebaseio.com';
    DSFirebaseAdapterProvider.defaults.basePath = basePath;

    $routeProvider.when('/user/:id', {
      controller: 'UserCtrl',
      controllerAs: 'UserCtrl',
      template: 'routes/user/user.html',
      resolve: {
        user: function ($route, User) {
          return User.find($route.current.params.id);
        }
      }
    });
  })
  .run(function (DS, DSFirebaseAdapter, User) {

    // js-data-angular created a new store
    // automatically and registered it as DS.
    // The firebase adapter was already registered,
    // but we want to make it the default.
    DS.registerAdapter(
      'firebase',
      DSFirebaseAdapter,
      { default: true }
    );
  })
  .service('User', function (DS, DSFirebaseAdapter) {
    return DS.defineResource('user');
  })
  .controller('UserCtrl', function ($scope, $routeParams, User, DSFirebaseAdapter) {
    var UserCtrl = this;

    // Update the user on the controller whenever
    // the user changes
    User.bindOne({}, $scope, 'UserCtrl.user');

    function handleUserChange (dataSnapshot) {
      var user = dataSnapshot.val();
      if (user[Resource.idAttribute] && user[Resource.idAttribute] == $routeParams.id) {
        User.inject(user);
      }
    }

    var ref = DSFirebaseAdapter.ref.child(Resource.endpoint);
    ref.on('child_changed', handleUserChange);

    $scope.$on('$destroy', function () {
      ref.off('child_changed', handleUserChange);
    });
  });
Nils Ivanson
@nivanson
Jun 20 2015 21:21
@jmdobry Thanks for the help on beforeInject. It worked just the way I hoped. Now I have a date object instead no matter if I get with findAll, findOne, or sync from store with get.
Jason Dobry
@jmdobry
Jun 20 2015 23:01
I not sure why you even have to do that...