Monday 15 June 2015

knockout.js - "object is not a function" error when try to get value by calling WebAPI in KonckoutJS -


I'm working with KnockoutJS to create an online tool. Inside the visual model there is a calculated property that a WebAPI tells to get the remote counting values; My approach is trying to call the web API via jquery .ajax, as shown below the code snippet

This "item generates no function on the spot" error where I try to move the parameters, which is the one inside the same visual model Tax for Vebapi.

Please show me a way to do it right, thank you

  & lt; Input id = "interestRate" type = "text" data-bind = "value: interestRate" /> & Lt; Input ID = "zodiac living withdraw" type = "text" data-bind = "value: zodiac biweeddrawan" /> & Lt; Strong id = "calculatedFee" data-bind = "text: calculate result (). CalculFee" & gt; & Lt; / Strong> & Lt; Strong id = "adminFee" data-bound = "text: calculate security (). Interest" & gt; & Lt; / Strong>  
  on apiUrl = "/ API / calculator" var calculatorModel = function () {this.interestRate = ko.observable (7.5) ; This.amountBeingWithdrawn = ko.observable (10000); ApiUrl, type: 'obtaining' datatype ':' json ', data: {interestRateContracted: this.calculateResult = ko.computed (function () {$ .ajax ({url returned this.interestRate (), // error: Uncaught writing error: The object is not a function AmountBeingWithdrawn: this.amountBeingWithdrawn () // Error: Uncaught TypeError: Object is a function}, the success is not: function (result) {return {fee: result.CalculatedFee, interest: result. interest };}}}}}}. Extension ({async: true});} Var calculator = new calculator model (); $ (document) .ready (function () {ko.applyBindings (calculator);}); < / Code> 

The problem is that this is something different, as you did not tell it anything, you have two options first, the on the external object Place the context of this and use it within the calculation:

  var calculator model = function () {// keep it in the context of the original "this" hanging around you Is = this; this.interestRate = ko.observable (7.5); This.amountBeingWithdrawn = ko.observable (10000); , ApiUrl type :: this.calculateResult = ko.computed (function () {$ .ajax ({url return 'received', datatype: 'json', data: {// Here we use the variable itself, which is still there // Rectify the right object InterestRateContracted: self.interestRate (), AmountBeingWithdrawn: self.amountBeingWithdrawn ()}, success: function (result) {return {fee: result.CalculatedFee, interest: result.Interest};}});} ) .extend ({async: true}); }  

Or, secondly, use the fact that it can be beaten:

  this.calculateResult = ko.computed (function () {return $ .ajax ({url: apiUrl, type: 'received', datatype: 'json', data: {InterestRateContracted: this.interestRate (), AmountBeingWithdrawn: this.amountBeingWithdrawn ()}, success: function (result) {return {Charges: Results .CalculatedFee, Interest: result.Interest}}}})}}, this) .extend ({async: true}); // Note to calculate this parameter on the above line / calculation  

No comments:

Post a Comment