Announcement

Collapse
No announcement yet.

Set API result into model

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

  • 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.

  • #2
    'this' is not available in the function

    Comment


    • #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


      • #4
        Hello Team,

        Anyone have solutions?

        Please let me know.

        Thanks.

        Comment


        • #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...
        X