我的問題是有點類似跟隨着懸而未決的問題。 (不知道雖然) Sitecore 8 SPEAK: Getting an Error When calling a Method in JS FileSitecore的8 SPEAK - 調用自定義組件上的JavaScript方法
我使用Sitecore8
在我的頁面上有一個按鈕,並在其click事件我要調用add()的自定義數據源組件。
佈局:
JS代碼的頁面:
define(["sitecore"], function (Sitecore) {
var JsonListPage = Sitecore.Definitions.App.extend({
initialized: function() {
alert('Inside Json PageList Init');
},
loadData: function() {
alert('Button clicked');
app.add();
}
});
return JsonListPage;
});
JS代碼自定義數據源組件:
define(["sitecore"], function (Sitecore) {
var model = Sitecore.Definitions.Models.ControlModel.extend({
initialize: function (options) {
this._super();
this.set("json", null);
alert('Inside Jsondatasource Init');
},
add: function (data) {
var json = this.get("json");
if (json === null)
json = new Array();
// this is done because array.push changes the array to an object which then do no work on the SPEAK listcontrol.
var newArray = new Array(json.length + 1);
for (var i = json.length - 1; i >= 0; i--)
newArray[i + 1] = json[i];
newArray[0] = data;
this.set("json", newArray);
}
});
var view = Sitecore.Definitions.Views.ControlView.extend({
initialize: function (options) {
this._super();
this.model.set("json", null);
}
});
Sitecore.Factories.createComponent("JsonDatasource", model, view, ".x-sitecore-jsondatasource");
});
.cshtml自定義組件:
@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecore.Web.UI.Controls.Common.UserControls
@model RenderingModel
@{
var userControl = Html.Sitecore().Controls().GetUserControl(Model.Rendering);
userControl.Requires.Script("client", "JsonDatasource.js");
userControl.Class = "x-sitecore-jsondatasource";
userControl.Attributes["type"] = "text/x-sitecore-jsondatasource";
userControl.DataBind = "Json: json";
var htmlAttributes = userControl.HtmlAttributes;
}
<div @htmlAttributes>
am here again
</div>
頁面加載時:
- 它顯示警報從自定義組件INIT
- 然後顯示了從主機頁面的Init警報
- 在按鈕單擊它顯示警報,之後給出了「錯誤應用」。
有一點,我失蹤..任何幫助將不勝感激..請讓我知道,如果你需要任何輸入。
在此先感謝!因此,使用ID避免這種情況,用「這個」,而不是
非常感謝您!我已經在編寫應用程序,查看您提供的第二個鏈接。 出於某種原因,我的變化做出的Javascript不會反映除非我清除瀏覽器緩存。對此有何想法?或者我應該爲此發佈一個不同的問題? –
預期是啊,多數民衆贊成,Sitecore的緩存所有的JavaScript非常好。 – Komainu85