Announcement

Collapse
No announcement yet.

How to get model of entity by id in client js ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get model of entity by id in client js ?

    PHP Code:

     
    var entity = {scope:"Call"id:"12a4c6b8ff8ad5c"};
     var 
    model = ??????; // what do I put here ? 

    some js code uses something like the following, but I do not understand if/where the id is passed
    PHP Code:

    this
    .getModelFactory().create(entity.scope, function (model) {
       
    // ....
    }); 

    I think I need to trigger the GET api call for that entity somehow
    Last edited by tothewine; 01-30-2019, 12:42 AM.

  • #2
    HTML Code:
    this.getModelFactory().create('MyEntityType', function (model) {
         model.id = someId;
         model.fetch().then(function () {
               console.log(model); // it's fetched
         }.bind(this));
    });  

    Comment


    • #3
      Thanks! Now I understand that. I hope this thread becomes canonical documentation.

      Could you also tell me how waiting works? Like the last parameter of createView and this.wait.
      What do they do exactly and how are they used?

      Comment


      • #4
        There gonna be a documentation about waiting.

        The problem is that sometimes we need to run some async code before view is rendered. By default a view is rendered after setup method is executed and all views created w/ createView method are loaded. The most rough way is to use wait function with a boolean argument.


        HTML Code:
        setup: function () {
            this.wait(true); // start waiting
            this.model.fetch().then(function () {
                  // do something if needed
                  this.wait(false); // stop waiting
            }.bind(this));
        }
        In the doc it will be described how to use promises for waiting.

        Wait argument in createView is not recommended to use.
        Last edited by yurikuzn; 01-30-2019, 06:03 PM.

        Comment


        • tothewine
          tothewine commented
          Editing a comment
          Thanks, you are enlighten me every time

      • #5
        Originally posted by yurikuzn View Post

        In the doc it will be described how to use promises for waiting.

        Wait argument in createView is not recommended to use.
        What document are you talking about? Please give a link.

        Comment


        • tothewine
          tothewine commented
          Editing a comment
          It was upcoming, I'm unsure if it is written yet

      • #6
        yurikuzn I still have a doubt... are client-side models cached? What if I want to obtain a model instance without doing a server synchronization (avoiding the additional GET request) ?

        Comment

        Working...
        X