我正在编写一些代码,这些代码使用SharePoint 2013中的jquery和handlebar来获取用户名。我创建了一个测试帐户,并尝试检索配置文件详细信息,但是我遇到了问题"未定义未捕获的引用错误(函数)。我已经评论了我的问题发生的路线。
; (function ($, window, document, undefined) {
//creates the default data
var pluginName = "DisplayUserData",
defaults = {
user: null
};
//plugin ctor
function DisplayUserData(element, options) {
var that = this;
that.element = element;
that.$element = jQuery(element);
that.options = $.extend({}, defaults, options);
that.defaults = defaults;
that.styleLibraryPath = _spPageContextInfo.siteAbsoluteUrl + "/SiteAssets/userdata/displaydata";
that.init();
}
DisplayUserData.prototype = {
init: function () {
var that = this;
if(typeof that.options.user != null) {
var templateCall = that.getTemplate(that.options.template);
templateCall.complete(function (date) {
that.mix(data); //here is my issue
});
}
},
getTemplate: function (template) {
var that = this;
return jQuery.ajax({
url: that.styleLibraryPath +"/" + template,
type: "GET",
cache: true,
data: {},
dataType: "html",
contentType: "text/html"
});
},
mix: function (data) {
var that = this;
template = data,
results = that.getValueByKey(that.options.property, that.options.user);
if (results !== "" && typeof results != 'undefined') {
that.display(results, template);
}
},
getValueByKey: function (key, obj) {
var data = jQuery.grep(obj.UserProfileProperties, function (item) {
return item.Key == key;
});
if (data.length > 0) {
var value = data[0].Value;
if (key === "Title") {
var titleSegments = value.split(" ");
value = titleSegments[titleSegments.length - 1];
}
return value;
}
},
display: function (results, template) {
var that = this;
var compiledTemplate = Handlebars.compile(template);
var outputHtml = compiledTemplate(results);
that.$element.html(outputHtml);
}
};
//plugin wrapper prventing multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
$.data(this, "plugin_" + pluginName,
new DisplayUserData(this, options));
});
};
}) (jQuery, window, document);