Giter VIP home page Giter VIP logo

Comments (3)

jmdobry avatar jmdobry commented on May 30, 2024

But how should an existing item in the DataStore be updated via the synchronous API?

Use DS#inject. If the item is already in the store than what you're injecting will be merged with the existing item. It's the perfect way to update what's in the data store.

Here's an example of how I do this in one of my apps:

angular.module('app').service('Msg', function ($rootScope, DS) {
  'use strict';

  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);
  });

  // I don't send the actual item up the socket, just the id and resource name
  socket.on('update', function (data) {
    // Get the latest copy of the item if it's in the store
    DS.refresh(data.resource, data.id);
    $rootScope.$broadcast('update', data.resource, data.id, data.ownerId);
  });

  // This relates to your example (full payload sent up the socket)
  socket.on('update_full', function (data) {
    // Inject the data, updating any existing items
    DS.inject(data.resource, data.payload);
    $rootScope.$broadcast('update_full', data.resource, data.payload);
  });

  socket.on('destroy', function (data) {
    DS.eject(data.resource, data.id);
    $rootScope.$broadcast('destroy', data.resource, data.id, data.ownerId);
  });

  return {};
});

Edit: The above gives my app a bit of "3-way data-binding".


Closing because Iā€™m not sure this is an issue, if you are convinced that this is really a bug, please feel free to re-open the issue and add more information:

  • good - Your versions of Angular, JSData, etc, relevant console logs/error, code examples that revealed the issue
  • better - A plnkr, fiddle, or bin that demonstrates the issue
  • best - A Pull Request that fixes the issue, including test coverage for the issue and the fix

Otherwise support is done via the mailing list.

from js-data.

reppners avatar reppners commented on May 30, 2024

Super easy as I expected :) Thanks :)

from js-data.

jmdobry avatar jmdobry commented on May 30, 2024

:)

from js-data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.