Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: [DHTML] Flexible AJAX table - DBGrid

  1. #11
    Join Date
    Dec 2007
    Location
    Ankara, Turkey
    Posts
    160
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    Ok. Now, as stated in the above message, switched to prototype based definitions AND removed all cyclic references under the name of creator.

    There a few minor issues about paging when the filtering or grouping is active, will fix them soon.

    Do you see any more memory leaks Twey?

  2. #12
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I did point them out, but the explanation is long and involved, and judicious use of Google will get you far.

    I don't see any further obvious memory leaks, although it's getting late and my head hurts, so my soundness of judgment is currently questionable. There are some other poor programming practices in there, though, like looping over that huge object in unite() to no apparent purpose, and extending Object in the first place. See http://blog.metawrap.com/blog/June6t...renessDay.aspx.

    Here's another solution if you like extending stuff:
    Code:
    var using = (function() {
      var extensions = {}, global = this;
    
      function isApplied(extn) {
        var e = extn.split('.');
        return extensions[e[0]][e[1]] && global[e[0]].prototype[e[1]];
      }
    
      function apply(extn) {
        var e = extn.split('.');
        return global[e[0]].prototype[e[1]] = extensions[e[0]][e[1]];
      }
    
      function unapply(extn) {
        var e = extn.split('.');
        delete global[e[0]].prototype[e[1]];
      }
    
      function using() {
        var extns = Array.prototype.slice.call(arguments);
        return function(fn) {
          return function() {
            for (var i = extns.length; --i >= 0; )
              if (!isApplied(extns[i]))
                apply(extns[i]);
              else
                extns.splice(i, 1);
    
            var r = fn.apply(this, Array.prototype.slice.call(arguments));
    
            for (var i = extns.length; --i >= 0; )
              unapply(extns[i]);
    
            return r;
          };
        };
      }
    
      function add(c, o) {
        for (var x in o)
          if (o.hasOwnProperty(x))
            (extensions[c] || (extensions[c] = {}))[x] = o[x];
        return o;
      }
    
      using.add = add;
    
      return using;
    })();
    This permits you to write code like:
    Code:
    using.add("Array", {
      map: function(f, a) {
        for (var i = a.length, r = []; --i >= 0; )
          r[i] = f(a[i]);
        return r;
      },
    
      reduce: function(f, t, a) {
        for (var i = 0, n = a.length; i < n; ++i)
          t = f(t, a[i]);
        return t;
      }
    });
    
    // ... then use it:
    
    var sumDoubles =
    using("Array.map", "Array.reduce")
    (function(a) {
      return a.map(function(n) { return n * 2; }).reduce(function(a, b) { return a + b; });
    });
    
    // And call the function normally:
    
    sumDoubles([1, 2, 3]); // 12 (2 + 4 + 6)
    Last edited by Twey; 09-13-2008 at 08:55 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #13
    Join Date
    Dec 2007
    Location
    Ankara, Turkey
    Posts
    160
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    Thank you very much for the resources I'll look into them.

    I'm using unite to allow users to use an external configuration file so that they do not have to change the original JS file everytime it is updated. Unite assures that everything is covered up even if the user does not fill out every config area and does not override them. But extending Object class, well I thought about it, the only difficulty was that I had to use "hasOwnProperty" check in every "for in" loop but I do not know any disadvantages so I'll look into the article that you have given.

    Thanks again

    EDIT: I have read the article and again, if you use hasOwnProperty verification there is not much danger except that if you use any preserved name for a hash object as Dean Edwards stated. Will look into this and try to find an easy and compatible way for both of them
    Last edited by BYK; 09-21-2008 at 08:28 PM. Reason: Typo & Note

  4. #14
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Also that you're adding a property to the lookup chain of every object in the environment, which I suspect would cause some fairly considerable performance issues. The using() function I outlined above should be quite sufficient.

    Certainly you can use hasOwnProperty() to check, but you shouldn't have to — and more importantly, your users shouldn't have to. You are developing a library for reuse by yourself and other users: it's up to you to make it play nicely. I personally don't bother extending built-ins any more without careful use of using(): it's messy. Simple code like:
    Code:
    if ('foo' in bar)
    becomes
    Code:
    if ('foo' in bar && bar.hasOwnProperty('foo'))
    while a simple for..in loop
    Code:
    for (var x in bar)
    becomes
    Code:
    for (var x in bar)
      if (bar.hasOwnProperty(x))
    Nasty, verbose, easy to forget, and irritating in the cases where there really are properties one would like to add to the prototype that one would prefer to be able to iterate over. If you want to deal with this in your own code, go ahead, but limit it with using() so other people don't have to.

    Oh, minor hint:
    Code:
    foo = foo ? foo : bar;
    // can become: foo = foo || bar;
    
    if (!foo)
      foo = bar;
    // can become: foo = foo || bar;
    
    if (!foo)
      foo = {};
    foo = foo.unite(bar);
    // can become: foo = (foo || {}).unite(bar);
    Last edited by Twey; 09-14-2008 at 01:16 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #15
    Join Date
    Dec 2007
    Location
    Ankara, Turkey
    Posts
    160
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default

    Actually, moving a few functions outside the Object prototype won't hurt anyone so I think I'll simply make them functions which are taking the objects as their first parameters. Simple and effiicient enough I think?

  6. #16
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Indeed. Greatly preferable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •