将数组传递给Javascript函数

时间:2022-12-05 16:01:49

I'm using a web service to populate a selection list and I now need to do the same for a number of selection lists ideally using the same method to try and limit the amount of code. Here's the method I use to make my web service call:

我正在使用Web服务来填充选择列表,我现在需要对许多选择列表执行相同操作,理想情况下使用相同的方法来尝试限制代码量。这是我用来进行Web服务调用的方法:

function GetColourReferences(self) {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        url: '/GetColourReferences',
        success: function (data) {
            self.colourReferences(data);
        }
    });
}

I've tried something similar to the following but I can't get it to work - is it even possible?

我尝试过类似以下的东西,但我无法让它起作用 - 它甚至可能吗?

function GetReferences(self, list, refUrl) {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        url: refUrl,
        success: function (data) {
            list(data);
        }
    });
}

Here's how I'd call it (I'm using Knockout):

这是我如何称呼它(我正在使用Knockout):

GetReferences(self, self.colourReferences, '/GetColourReferences');

Thanks for looking :)

谢谢你看:)

1 个解决方案

#1


0  

1)Dude whatever your excuse listen to the tips from the others NEVER NEVER use Sync call especially if your backend is slow to process or if you have some messy ui requirement.

1)老兄,无论你的借口听取其他人的提示,永远不要使用同步电话,特别是如果你的后端处理速度慢或者你有一些麻烦的ui要求。

2) sort out your closure and learn how to scope method of an object and you will fix your problem but I'm going to be nice and point you in the right direction.

2)理清你的闭包并学习如何定位一个对象的方法,你将解决你的问题,但我会很好,并指出你正确的方向。

3) This don't work simply because you pass a reference to the function and not a reference to the method of the object so cleverly called self (WTF have you heard of semantic) That aside list is function that has lost the object scope and this is where the problem is!

3)这不起作用只是因为你传递对函数的引用而不是对象的方法的引用如此巧妙地称为self(WTF你听说过语义)那旁边的列表是失去了对象范围的函数这就是问题所在!

function GetReferences(self, list, refUrl) {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        url: refUrl,
        success: function (data) {
            list.call(self, data);
        }
    });
}

Hope that help.

希望有所帮助。

#1


0  

1)Dude whatever your excuse listen to the tips from the others NEVER NEVER use Sync call especially if your backend is slow to process or if you have some messy ui requirement.

1)老兄,无论你的借口听取其他人的提示,永远不要使用同步电话,特别是如果你的后端处理速度慢或者你有一些麻烦的ui要求。

2) sort out your closure and learn how to scope method of an object and you will fix your problem but I'm going to be nice and point you in the right direction.

2)理清你的闭包并学习如何定位一个对象的方法,你将解决你的问题,但我会很好,并指出你正确的方向。

3) This don't work simply because you pass a reference to the function and not a reference to the method of the object so cleverly called self (WTF have you heard of semantic) That aside list is function that has lost the object scope and this is where the problem is!

3)这不起作用只是因为你传递对函数的引用而不是对象的方法的引用如此巧妙地称为self(WTF你听说过语义)那旁边的列表是失去了对象范围的函数这就是问题所在!

function GetReferences(self, list, refUrl) {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        url: refUrl,
        success: function (data) {
            list.call(self, data);
        }
    });
}

Hope that help.

希望有所帮助。