开发者

Javascript override problem class xml

Not sure what is the problem , this the second post looking for the answer.. but this time with a with the example .

What i'm doing : I 'm implementing a gallery that is getting a xml, and then build me using some javascript code. the problem i tried to call twice gallery.init like :

$(document).ready(function(){
   galleryXML.init({
       id: "#gallery1"
   });
   galleryXML.init({
       id: "#gallery"
   }); 
})

I expected to have one in #ga开发者_运维问答llery1 other in #gallery. Can someone tell me what the problem(it only happen when i had the loadXml() , so probably something with asynchronous call not sure )?


I think your problem can be that you are using the same variable _P for (what you expect to be) 2 different instances of the galleryXML.

The _P variable is created and initialized when the javascript code is parsed, because of the () after the var galleryXML = function() {...}.

So I guess your problem is going to be solved if you just put the variable inside the init of galleryXML. You can see the code here: jsfiddle.net/rpNab/3/ (notice that now each li is inside each gallery, instead of both li in the last gallery)

EDIT: And I realize that now with my modification the galleryXML module seems ugly (because it only has one method and no variables), so I made a minor refactoring in order to have more methods inside that class, but the methods now must receive the parameter because the class itself continue to be "static", but the parameters can make it act for different contexts. Hope it helps: jsfiddle.net/rpNab/4/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜