Git Product home page Git Product logo

Comments (1)

yasserf avatar yasserf commented on May 23, 2024

Thanks for the feedback, much appreciated!

I'll divide your questions into a few parts, they are also ordered a bit different to in your question.

I don't understand why a message reading the record is setting its data, or am I misinterpreting something?

When a record is requested from deepstream it tries to load its current state, and if the record doesn't exist will create it as an empty object and return that value.

Once that value is retrieved it will then fire the whenReady event, which means the record has set its data to be in sync with what the server has and you can start using it.

Since requesting the record the first time is an async operation, this._$data will be updated with the current state of the record from the server as soon as it is loaded, which is why it is being overriden on read events.

Produces an empty object rather than an empty array

The way lists work is they provide convenience APIs ontop of a normal record. This is to make it easier than having to deal with arrays directly and provide extra callbacks.

What you raised is actually a bug in the API where it exposes the default record content when it's first created ( which is an empty object ).

The method getEntries could instead be used to return an array instead so your code would look like this:

var shrubs =ds.record.getList('shrubs');
*wait a bit, or wrap in .whenReady()*
shrubs.subscribe(function(shrubEntries) { console.log('shrubEntries', shrubs.getEntries() ) }, true);
Doesn't log anything unless the subscribe is separated and wrapped in a .whenReady()

This is intended behaviour, since no events are actually fired until the isReady event occurs.

In your example:

ds.record.getList('shrubs').subscribe(function(shrubEntries) { console.log('shrubEntries', shrubEntries) }, true);

Since shrubEntries is not yet loaded from the server, you can't actually be told what the current state is.

By putting it in whenReady:

ds.record.getList('shrubs').whenReady( function() {
    subscribe(function(shrubEntries) { console.log('shrubEntries', shrubEntries) }, true);
} );

It's now saying that as soon as the information is loaded from the server ( its initial state ) subscribe to the any changes and be told what the initial value is.

In your case of finding out if it's loaded or not would be:

// Set Default Loading Behaviour
var list = ds.record.getList('shrubs');
list.whenReady( function() {
    subscribe( function( shrubEntries ) { 
        var shrubEntries = list.getEntries();
    }, true);
        // Remove loading behaviour ( this should only be triggered once during the list lifetime )
} );

Normally, using records, you won't need to use closures, but given the issue mentioned in part 2 the record is still exposed in a few callbacks instead of the list. Raised #33 as well to capture that.

Hope this helps, and feel free to let me know if something doesn't make sense!

from deepstream.io-client-js.

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.