Javascript coding patterns / best practise

  1. #1

    Javascript coding patterns / best practise

    Hi,

    I've been using Ext.NET for a few months now, and I'm finding that my javascript files are starting to get heavy and quite brittle, so I was thinking about refactoring them a bit. I've been looking at some of the new js frameworks / libs out there like backbone.js and knockout.js, and wondered if they could be useful in the context of the UI-oriented JS that we use in ext.net...

    Current issues I have with my JS files:

    • No seperation of concerns - a few JS classes containing code for UI, data operations, etc
    • Cunfusion around "this" - since ExtJS usually changes the context of this when an event calls a method in my class, it prevents me from being able to call "this.otherMethod" from my classes
    • Tight coupling - no testability


    So, I was wondering if there are any best practice or thoughts on this. In hindsight, I think a better approach would be to separate out all of the code that gets data from UI elements into a separate class that populates a sort of view-model, and then use that view model in my other classes which perform operations on that code - this would help with testability too.

    Any thoughts/ideas welcome!

    (Please feel free to move to a more appropriate area in the forum if necessary)

    Thanks
    Matt
  2. #2
    Hi,

    Quote Originally Posted by mattwoberts View Post
    No seperation of concerns - a few JS classes containing code for UI, data operations, etc
    Well, ExtJS team separated everything as much as they can. And ExtJS class is a very good example how to separate functionality in JavaScript.

    You can consider GridPanel + Store + GridView + SelectionModel (Row- or Checkbox-) classes.

    Quote Originally Posted by mattwoberts View Post
    Cunfusion around "this" - since ExtJS usually changes the context of this when an event calls a method in my class, it prevents me from being able to call "this.otherMethod" from my classes
    There is the .createDelegate() function in ExtJS which can help.
    http://dev.sencha.com/deploy/ext-3.4...createDelegate

    Also the native function .call() and apply() can help as well.

    Quote Originally Posted by mattwoberts View Post
    Tight coupling - no testability
    There are some unit testing frameworks, please see:
    http://stackoverflow.com/questions/3...unit-test-tool
  3. #3
    Thanks for the input, I'll take a look at the ExtJS source code as you suggest.

    The issue I had with testability was not that I didn't know about the testing frameworks available, but more that the way my JS files were created meant that it was impossible to test because of the tight coupling between the JS and ExtJS - which is why view models would help for me.

    Perhaps moving this thread to a more "general" section of the forum might be a good idea in case other people have good experience to share with this subject too?

    Thanks
  4. #4
    Moved to "Open discussions".

Similar Threads

  1. [CLOSED] Coding a "Display Once" tooltip
    By jwf in forum 1.x Legacy Premium Help
    Replies: 13
    Last Post: May 21, 2011, 6:59 AM
  2. mvc patterns for firing actions
    By costab in forum 1.x Help
    Replies: 0
    Last Post: May 13, 2011, 10:58 PM
  3. [CLOSED] How to know if a FormPanel is valid by coding?
    By flormariafr in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jun 14, 2010, 9:07 PM
  4. Replies: 1
    Last Post: Oct 16, 2009, 4:59 PM
  5. [CLOSED] How to add ext.msg.alert in C# coding...
    By king1231986 in forum 1.x Help
    Replies: 4
    Last Post: Nov 19, 2008, 5:35 AM

Posting Permissions