javascript - JqueryUI Own widget: Instances overwrite values (sometimes) -
i wrote little test describe problem:
<div id="test1"></div> <div id="test2"></div> <script> (function ($) { $.widget("mseh.testit", { options : { foo : false, bar : false }, _settedoptions : { foo : false, bar : false }, readoptions : function() { homecoming this._settedoptions; }, _init : function() { this._initializetest(); }, _initializetest : function() { var self = this; $.each(self.options, function(key, value) { self._settedoptions[key] = value; }); } }); })(jquery); $('#test1').testit({ foo : true, bar : false}); $('#test2').testit({ foo : true, bar : true}); console.log($('#test1').testit('readoptions')); console.log($('#test2').testit('readoptions')); </script> in illustration both logs give me true 'foo' (as expected) , true 'bar' (not expected).
is bug or doing wrong?
tested jquery 1.7.2 , 1.9.1. tested jqueryui 1.9.2 , 1.10.0.
thanks
because _settedoptions shared in every instance of testit.
if check _settedoptions's object equality between test1 , test2 below, refers same object.
console.log( $('#test1').testit('readoptions') === $('#test2').testit('readoptions') ); if want treat instance-variable, write below.
(function ($) { $.widget("mseh.testit", { options : { foo : false, bar : false }, // field shared in every instance. // _settedoptions : { // foo : false, // bar : false // }, readoptions : function() { homecoming this._settedoptions; }, _init : function() { // this!!!!! this._settedoptions = { foo : false, bar : false }; this._initializetest(); }, _initializetest : function() { var self = this; $.each(self.options, function(key, value) { self._settedoptions[key] = value; }); } }); })(jquery); javascript jquery jquery-ui jquery-ui-widget-factory
No comments:
Post a Comment