Set API result into model

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VigneshChinnaiyan
    Member
    • Jan 2018
    • 61

    Set API result into model

    Hello,

    In phone.js

    I am using this API to get the user Object.

    $.getJSON('User/' + this.model.get('assignedUserId'), function (json) {
    this.model.set('confident',json.region);
    });

    How to set the result json.region into model?

    setup: function () {
    this.dataFieldName = this.name + 'Data';
    this.defaultType = this.defaultType || this.getMetadata().get('entityDefs.' + this.model.name + '.fields.' + this.name + '.defaultType');

    if (this.model.has('doNotCall')) {
    this.listenTo(this.model, 'change:doNotCall', function (model, value, o) {
    if (this.mode !== 'detail' && this.mode !== 'list') return;
    if (!o.ui) return;
    this.reRender();
    }, this);
    }

    $.getJSON('User/' + this.model.get('assignedUserId'), function (json) {
    this.model.set('confident',json.region);
    });


    },

    I get the ERROR in console : Uncaught TypeError: Cannot read property 'set' of undefined

    How to fix this?

    Thanks.
    Last edited by VigneshChinnaiyan; 07-15-2018, 03:04 AM.
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    'this' is not available in the function

    Comment

    • VigneshChinnaiyan
      Member
      • Jan 2018
      • 61

      #3
      Hi Tanya,

      Added this.

      $.getJSON('User/' + this.model.get('assignedUserId'), function (json) {
      this.model.set('confident',json.region);
      }, this);

      ---- STILL I GET THE SAME ERROR in CONSOLE----


      Please give me the solution to fix that, You only asked me to set the API result into a userModel.

      Now i am asking you to help in setting the result object to that model.

      You just replied why my code is not working, But i didn't ask for that.

      Please do understand that all developers in your forum are not same in their expertise.

      So please guide them to resolve the issue if they ask for help.

      Thanks.
      Last edited by VigneshChinnaiyan; 07-19-2018, 12:24 AM.

      Comment

      • VigneshChinnaiyan
        Member
        • Jan 2018
        • 61

        #4
        Hello Team,

        Anyone have solutions?

        Please let me know.

        Thanks.

        Comment

        • telecastg
          Active Community Member
          • Jun 2018
          • 907

          #5
          Had a similar situation, needed to invoke this.getModelFactory() method inside an ajax call:

          Code:
                           var jqxhr = $.ajax(url).done(function (data) {
                                          this.linkDisplayEntity = data['parentType'];   
                                          this.linkDisplayEntityId = data[this.linkDisplayAttribute];     
                                          alert("this.getModelFactory() = "+this.getModelFactory()); // displays " [object Object] " so 'this' is now available inside the function
                                      }.bind(this));
          That worked for me, try:

          var jqxhr = $.getJSON('User/' + this.model.get('assignedUserId'), function (json) {
          this.model.set('confident',json.region);
          } .bind(this));

          Hope it helps.

          Comment


          • VigneshChinnaiyan
            VigneshChinnaiyan commented
            Editing a comment
            Thanks MUCH, Let me try and get back to you.
        Working...