building jQuery plugin , Object is not passing to 'each' function -
i'm building jquery plugin . initial code :
if (typeof object.create !== 'function') { object.create = function(obj) { function f() {}; f.prototype = obj; return new f(); }; }(function($, window, document, undefined) { var datatable = { init: function(options, elem) { console.log(options); var self = this; self.elem = elem; self.$elem = $(elem); // if (typeof options !== 'string') { self.options = $.extend({}, $.fn.sonaldatatable.options, options); // } console.log(self.options); self.cycle(); }, cycle: function() { var self = this; self.buildtable(); }, buildtable: function() { var self = this; self.gettableinfo(); }, gettableinfo: function() { var self = this; $.getjson(self.options.url + 'initiate', function(data) { // this.options.table = data; console.log(self.options); }); } }; $.fn.sonaldatatable = function(opt) { console.log(opt); // pos1 : shows every object pass { test : 'it real test'} return this.each(function(opt) { console.log(opt); // pos2 : , shows 0; var datatable = object.create(datatable); datatable.init(opt, this); //console.log(datatable); }); }; $.fn.sonaldatatable.options = { create: 'new', delet: 'trash', url: window.location.href, table: { limit: 10 } }; })(jquery, window, document);
in above code there 2 places in comments named pos1: , pos2: , see in pos1 can see ever pass plugin in console log @ pos2 can see 0 in teh console log. what's problem . whay can not pass 'opt' 'each' function ?
problem in part of code :
$.fn.sonaldatatable = function(opt) { console.log(opt); // pos1 : shows every object pass { test : 'it real test'} return this.each(function(opt) { console.log(opt); // pos2 : , shows 0; var datatable = object.create(datatable); datatable.init(opt, this); //console.log(datatable); }); };
my problem can not pass 'opt' object 'this.each' function.
i'll best describe why you're seeing expected.
lets start here:
$.fn.sonaldatatable = function(opt) { console.log(opt); // pos1 : shows every object pass { test : 'it real test'} ...
lets remove context of jquery plugin , make old function
function sonaltestfunction(opt) { console.log(opt); }
here have function takes single parameter opt
, whatever passed in written console.
sonaltestfunction(42); // writes "42" console sonaltestfunction({foo:"bar"}); // writes "{foo:"bar"}" console
now, lets @ second function, again in total isolation:
return this.each(function(opt) { console.log(opt); // pos2 : , shows 0; });
in case you're calling jquery each
function, takes "callback" function called every element in jquery array. parameters of callback are
- the index of item
- the item (not seen in example)
this should tell why you're seeing output "0" here - theremust 1 element in array you're calling each
on, , it's index 0
.
you have re-used parameter name, fine, you're doing nested within first function - suspect expected value passed through, not - instead second usage hides first.
insofar fixing code, expect trying this:
$.fn.sonaldatatable = function(opt) { console.log(opt); // pos1 : shows every object pass { test : 'it real test'} return this.each(function(i) { // note use of more appropriate variable, not hide earlier use of opt console.log(opt); // pos2 : show expect var datatable = object.create(datatable); datatable.init(opt, this); }); };
Comments
Post a Comment