在迁移到requirejs时,我遇到了一个我似乎无法找到答案的问题。
问题是我的嵌套函数无法访问传递给requirejs回调的参数。
例如:
define(['knockout', 'knockoutmapping', 'other'], function(ko, mapping, other) {
var Something = function() {
var self = this;
self.items = ko.observableArray([]);
self.doSomeStuff = function(data) {
// I would think I would still be able to access
// parameters ko, mapping and other
// trouble is, I cannot. I only have
// a valid 1st parameter, "ko".
var d = ko.toJSON(self.items()); // this works fine
self.items(mapping.fromJS(data));// this does not work (undefined)
// do not have access to "other" either.
};
};
return {
Something: Something,
};
});
任何有关此的帮助将不胜感激。我知道我可以把这些变成回调中的变量,但这对我来说似乎不是正确的方法。
提前谢谢。
答案 0 :(得分:1)
在define中,回调是第一个参数,它有一个参数需要函数,试试:
define(function(require) {
var knockout = require('knockout');
var knockoutmapping = require('knockoutmapping');
var other = require('./other');
var Something = function() {
...
};
return {
Something: Something,
};
});