问题
When I calling a method in JS file from Sitecore SPEAK application once a button clicked, I'm getting the following error message:
TypeError: this.product is not a function this.product()
this.product() is the function name:
define(["sitecore"], function (Sitecore) {
var model = Sitecore.Definitions.Models.ControlModel.extend({
initialize: function (options) {
this._super();
},
products: function () {
var input = this.get("input");
$.ajax({
url: "/api/sitecore/Product/Find",
type: "POST",
data: { input: input },
context: this,
success: function (data) {
this.set("output", data);
}
});
},
product: function () {
var input2 = this.get("input2");
$.ajax({
url: "/api/sitecore/Product/FindSingle",
type: "POST",
data: { input2: input2 },
context: this,
success: function (data) {
this.set("output2", data);
this.set("output3", data.TitleS);
}
});
return null;
},
});
var view = Sitecore.Definitions.Views.ControlView.extend({
initialize: function (options) {
this._super();
},
product: function () {
this.product();
}
});
Sitecore.Factories.createComponent("ProductSearch", model, view, ".sc-ProductSearch");
});
Calling the above method from SPEAK Button.Click event as: javascript:app.product();
What can we do to avoid such case?
回答1:
If this is your PageCode for your SPEAK application can you update the JavaScript to extend Sitecore.Definitions.App rather than Sitecore.Definitions.Models.ControlModel.extend?
Eg. define the SPEAK application like this.
define(["sitecore", "jquery", "underscore"], function (Sitecore, $, _) {
var SpeakExample = Sitecore.Definitions.App.extend({initialize: function (options) {
},
products: function () {
var input = this.get("input");
$.ajax({
url: "/api/sitecore/Product/Find",
type: "POST",
data: { input: input },
context: this,
success: function (data) {
this.set("output", data);
}
});
},
product: function () {
var input2 = this.get("input2");
$.ajax({
url: "/api/sitecore/Product/FindSingle",
type: "POST",
data: { input2: input2 },
context: this,
success: function (data) {
this.set("output2", data);
this.set("output3", data.TitleS);
}
});
return null;
},return SpeakExample;});
回答2:
Try this,
define(["sitecore"], function (Sitecore) {
var model = Sitecore.Definitions.Models.ControlModel.extend({
initialize: function (options) {
this._super();
app = this;
},
products: function () {}
});
}
And in the button click type: javascript:app.ProductSearch.product()
回答3:
Try to call this.model.product() instead of this.product() in the product method inside your view definition.
来源:https://stackoverflow.com/questions/30436263/sitecore-8-speak-getting-an-error-when-calling-a-method-in-js-file