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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tothewine
    Active Community Member
    • Jan 2018
    • 373

    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, 01:42 AM.
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    HTML Code:
    this.getModelFactory().create('MyEntityType', function (model) {
         model.id = someId;
         model.fetch().then(function () {
               console.log(model); // it's fetched
         }.bind(this));
    });  
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • tothewine
      Active Community Member
      • Jan 2018
      • 373

      #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

      • yuri
        Member
        • Mar 2014
        • 8440

        #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 yuri; 01-30-2019, 07:03 PM.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment


        • tothewine
          tothewine commented
          Editing a comment
          Thanks, you are enlighten me every time
      • mr2d2
        Senior Member
        • Apr 2017
        • 126

        #5
        Originally posted by yurikuzn

        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
      • tothewine
        Active Community Member
        • Jan 2018
        • 373

        #6
        yuri 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...