Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Передача параметров в плагин и namespacing (https://javascript.ru/forum/jquery/27800-peredacha-parametrov-v-plagin-i-namespacing.html)

Viper 25.04.2012 11:48

Передача параметров в плагин и namespacing
 
Столкнулся с такой проблемой.
Есть начальный код плагина

;(function($, window, document, undefined){
	var text = '',
		methods = {
			init: function(text, options){
				options = $.extend({}, $.fn.myPlugin.defaultOptions, options);
				console.log(text);

				return this;
			}
		}

	$.fn.myPlugin = function(text, method){
		if (text != '') {
			// Тут беда
			return methods['init'].apply(this, Array.prototype.slice.call(arguments, 1));
		} else {
			if (methods[method]) {
				return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
			} else if (typeof method === 'object' || ! method) {
				return methods.init.apply(this, arguments);
			} else {
				$.error('Method ' +  method + ' does not exist!');
			}
		}
	}

	$.fn.myPlugin.defaultOptions = {
		param1: false
	}
})(jQuery, window, document);


И вызов
$('body').myPlugin('foo', 'options': {
	param1: true
});


В консоли получаю
Код:

missing ) after argument list
$('body').myPlugin('foo', 'options': {

Как можно передать таким образом параметры в плагин?

DjDiablo 25.04.2012 21:25

Так параметры в функции на javascript непередают.
myPlugin('foo', 'options': { //ошибка
           param1: true
});


обычно просто
myPlugin('foo', {
           param1: true
});

ну а если всё же очень хочется передать имя передаваемого параметра то
myPlugin('foo', {'options':{
           param1: true
}});

Viper 26.04.2012 16:03

Спасибо, уже разобрался.


Часовой пояс GMT +3, время: 21:03.