0

I have the following code

var loyaltyObject = (function () {
  var data = null, vm, getLoyaltyUrl;

  function getData() {
    $.ajax({
      type: 'POST',
      url: getLoyaltyUrl,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: function (resp) {
        data = resp;
      },
      error: function (resp) {
        console.log('Error fetching offers!');
        console.log(resp);
      }// error(resp)
    });// $.ajax()  
  }

...

  vm = {
    getData: getData,
    getLoyaltyUrl: getLoyaltyUrl
  };

  return vm;
}());

on document.ready I call

function Init() {
    window.loyaltyObject.getLoyaltyUrl = '@Url.Action("GetLoyaltyData", "Orders")';
    window.loyaltyObject.getData();
}

window.loyaltyObject.getLoyaltyUrl is indeed the url but the internal getLoyaltyUrl is still undefined.

I read the following return a variable as a property about setting up a getter, but how would I perform a setter, what is the this or value I would be setting?

Also would it be comparable on most browsers?

6
  • 1
    FWIW, I think there is an expectations that properties which start with get... are actually functions. Commented Jul 10, 2017 at 23:47
  • 3
    What's the reason for not making the URL a parameter of getData? Commented Jul 10, 2017 at 23:50
  • vm has getLoyaltyUrl as a (public) property, no need for a setter. Or do you want to set it on loyaltyObject? Commented Jul 11, 2017 at 0:00
  • Just use the this.getLoyaltyUrl property inside the method and drop the local variable. Commented Jul 11, 2017 at 0:51
  • 1
    Making the URL a parameter seems to be more natural to me. It's more expressive. "I want to load data from this URL" vs "Set this property to a URL and then get the data". Commented Jul 11, 2017 at 18:08

3 Answers 3

2

You are assigning the value to vm.getLoyaltyUrl not to the private variable getLoyaltyUrl. That variable has no inheritance from the object vm so it never ever gets defined in your code

Just change:

url: getLoyaltyUrl

to

url: vm.getLoyaltyUrl

Simplified example

var loyaltyObject = (function () {
  var data = null, vm;

  function getData() {
   // switched out the ajax for a simple console.log()
   console.log(vm.getLoyaltyUrl)
  }

  vm = {
    getData: getData,
    getLoyaltyUrl: null
  };

  return vm;
}());
// following is exactly what you have
function Init() {
    window.loyaltyObject.getLoyaltyUrl = '@Url.Action("GetLoyaltyData", "Orders")';
    window.loyaltyObject.getData();
}

Init()

Sign up to request clarification or add additional context in comments.

Comments

1

but how would I perform a setter, what is the this or value I would be setting?

You would simply assign to the variable getLoyaltyUrl:

vm = {
  // ...
  set getLoyaltyUrl(value) {
    getLoyaltyUrl = value;
  },
}

You wouldn't use this in your case. value is the value assigned to the property.

Comments

0

I don't think there's any need for a custom property here. But you can look into Object.defineProperty or it's Reflect counterpart to change objects properties on the fly.

Object.defineProperty(obj, 'getLoyaltyUrl', {
    set: (value) => {
        // do something here, better be worth it.
    },
    get: () => {}
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.