Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检索SP列表数据延迟承诺_Javascript_Jquery_Sharepoint_Web Parts_Jquery Deferred - Fatal编程技术网

Javascript 检索SP列表数据延迟承诺

Javascript 检索SP列表数据延迟承诺,javascript,jquery,sharepoint,web-parts,jquery-deferred,Javascript,Jquery,Sharepoint,Web Parts,Jquery Deferred,我仍然在努力获得一系列在可视化Web部件上工作的延迟承诺。最终目标是将一些DIV标签的颜色更改为SPList中引用的颜色 我最初使用这段代码处理从列表中查找数据的单个请求,但现在我正在寻找一个请求链。第一个是从window.location.href属性查找子站点的名称。尽管这段代码正在检索子站点名称,但考虑到这一类型,这可能是我的第一个错误,也许我应该从SPWeb对象获取它。然后,第二个调用在where子句中使用该值从第二个调用中检索到另一个列表的正确数据 因此,该代码(如果有的话)目前起作用

我仍然在努力获得一系列在可视化Web部件上工作的延迟承诺。最终目标是将一些DIV标签的颜色更改为SPList中引用的颜色

我最初使用这段代码处理从列表中查找数据的单个请求,但现在我正在寻找一个请求链。第一个是从window.location.href属性查找子站点的名称。尽管这段代码正在检索子站点名称,但考虑到这一类型,这可能是我的第一个错误,也许我应该从SPWeb对象获取它。然后,第二个调用在where子句中使用该值从第二个调用中检索到另一个列表的正确数据

因此,该代码(如果有的话)目前起作用,但是间歇性的。我想我没有在正确的位置调用.resolve,幸运的是异步代码是否在调用线程使用预期结果之前完成。因此,它在处理器神的膝上,可能50:50工作。这至少证明了我的jQuery代码正在生成所需的结果

我有两个函数对两个不同的DIV元素执行几乎完全相同的操作。这就是我现在拥有的

function alterMenuColour(id) {
getMenuItemfromURLValue(window.location.href).done(function (urlSelection) {

    var colkey = 0; //this is the key for the list collection array.  This needs to be uneque for each different call to retrieveListItems.

    var promise = retrieveListItems('/sites/OMGIntranet/OMGCentral/', 'MenuItemList', '<View><Query><Where><Eq><FieldRef Name=\'MenuItem\'/><Value Type=\'Text\'>' + urlSelection + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>', 'Id,MenuColour,BarColour', colkey);

    var collMenuListItem = collListItem[colkey];

    promise.done(
        function (collMenuListItem) {

            var listItemEnumerator = collMenuListItem.getEnumerator();

            var oListItem;

            while (listItemEnumerator.moveNext()) {
                oListItem = listItemEnumerator.get_current();
            }

            var menus = getChildElementsByType(document, id, 'div');

            jQuery(menus).children("div").each(function () {
                jQuery(this).css("background", oListItem.get_item('MenuColour'));
            });

        },
        function (sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    );
});
}

function alterBarColour() {
getMenuItemfromURLValue(window.location.href).done(function (urlSelection) {

    var colkey = 1; //this is the key for the list collection array.  This needs to be uneque for each different call to retrieveListItems.

    var barpromise = retrieveListItems('/sites/OMGIntranet/OMGCentral/', 'MenuItemList', '<View><Query><Where><Eq><FieldRef Name=\'MenuItem\'/><Value Type=\'Text\'>' + urlSelection + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>', 'Id,MenuColour,BarColour', colkey);

    var collBarListItem = collListItem[colkey];

    barpromise.done(
        function (collBarListItem) {

            var listItemEnumerator = collBarListItem.getEnumerator();

            var oListItem;

            while (listItemEnumerator.moveNext()) {
                oListItem = listItemEnumerator.get_current();
            }

            var bar = document.getElementsByClassName('SectionMenuBar');

            jQuery(bar).each(function () {
                jQuery(this).css("background", oListItem.get_item('BarColour'));
            });

        },
        function (sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    );
});
}

最初,当我编写此函数时,唯一能够确定如何成功返回数据的方法是在名为collListItem的全局列表集合对象中。这是可怕的,我讨厌这个,一直试图删除它,但迄今为止没有成功。不过,它确实可以工作,读了又读,一些人建议,对象不能通过延迟方法返回,它们必须链接起来才能传递数据。这又让我想起了能够使用可重用代码。

我现在已经让它100%的时间都在工作,这是我的目标,但我仍然不满意它似乎不是优雅的代码,我仍然在为这个概念而挣扎

我变了,

function alterMenuColour(id) {
-    getMenuItemfromURLValue(window.location.href).done(function (urlSelection) {
+    getMenuItemfromURLValue(window.location.href, 3).done(function (urlSelection) {


这些.done方法的第二个参数似乎是错误处理程序,但这不是.done的工作方式。当然,您可以用这种方式指定其他处理程序,但它们都将是成功处理程序,而不是错误处理程序,并且都将接受与解析承诺时使用的值/对象完全相同的参数列表。语句var collMenuListItem=collListItem[colkey];,var collBarListItem=collListItem[colkey];和var collSelectionMenuAssignemntListItem=collListItem[colkey];它们是多余的。虽然collMenuListItem、collBarListItem和CollSelectionMenuAssignmentListItem稍后在各自的外部函数中使用,但它们都是内部函数的形式变量,因此与前面声明的变量不同。感谢Beetroot。是的,你说的对。完成了,我试着交换它们,正如你所看到的,我意外地得到了一个。完成了,还有一个。然后。读了一遍又一遍的定义之后,我仍然无法理解其中的区别。我还读到它们是可互换的,这是错误的,因为正如你所说的,官方文档给了它们不同的参数。我对javascript的理解仍然相当基础。Matthew,看看代码,我认为最好的可重用性是泛化枚举器对象的获取-即编写一个通用函数fetchEnumeratorcolkey{…},它返回一个emumerator的承诺。fetchEnumerator的调用方式和返回承诺的处理方式具体取决于三个函数alterMenuColour、alterBarColour和getMenuItemfromURLValue。进一步的推广可能会弄巧成拙。今天我学到了一些其他东西,我需要使用get_lookupValue更改$2d_1,因为动态属性名称更改为$2e_1,而生产系统没有发出警告。
//http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx
function retrieveListItems(siteUrl, list, calm, include, collkey) {
var deferred = $.Deferred();

var clientContext
if (siteUrl == null) {
    clientContext = sharePointCurrentClientContext();
} else {
    clientContext = new SP.ClientContext(siteUrl);
}

var oList = clientContext.get_web().get_lists().getByTitle(list);

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(calm);

if (typeof this.collListItem === 'undefined') {
    this.collListItem = [];
}

this.collListItem.add(collkey, oList.getItems(camlQuery));

if (include == null) {
    clientContext.load(collListItem[collkey]);
} else {
    clientContext.load(collListItem[collkey], 'Include(' + include + ')');
}

clientContext.executeQueryAsync(Function.createDelegate(this, function () { deferred.resolve(collListItem[collkey]) }), Function.createDelegate(this, function (sender, args) { deferred.reject(sender, args); }));

return deferred.promise();
}
function alterMenuColour(id) {
-    getMenuItemfromURLValue(window.location.href).done(function (urlSelection) {
+    getMenuItemfromURLValue(window.location.href, 3).done(function (urlSelection) {
function alterBarColour() {
-    getMenuItemfromURLValue(window.location.href).done(function (urlSelection) {
+    getMenuItemfromURLValue(window.location.href, 4).done(function (urlSelection) {
-function getMenuItemfromURLValue(url) {
-
-    var colkey = 2; //this is the key for the list collection array.  This needs to be uneque     for each different call to retrieveListItems.
+function getMenuItemfromURLValue(url, colkey) {