kernel questions

67 views
Skip to first unread message

justin worsdale

unread,
Jan 11, 2013, 3:36:19 PM1/11/13
to kerneljs

Hi Alan,

I'm a Developer with Victoria's Secret and recently Nicholas Zakas has been consulting for us. In discussing architecture for our own site Kernel came up and I've been working with it, possibly to make a POC for using it on some work over here. I made a tiny project which is pretty similar to the first example on the Kernel site. I was hoping you'd answer a few questions.


  • I am trying to relate this to an MVC pattern. Is that a fair analogy or should I drop it? If it is, should I think of the Hub as the model?
  • Say I have global static vars– which I pretty much always do. Is it better to extend Kernel to have a Globals object, or would it make more sense to put those Globals in the hub when I define it?
  • When creating an api for myself, such as to create a Utils object– should that be part of the hub or part of Kernel? I'd be referencing it from my modules so I am thinking perhaps it should live in the hub. 
  • If I am abstracting a base library would I first abstract in Kernal then reference it in the hub?

I've attached my project incase it helps for reference.


--
Justin Dean Worsdale
www.justindeanworsdale.com
443.326.9348
im: jdeanworsdale
kernelproj.zip

alindsay55661

unread,
Jan 11, 2013, 6:00:22 PM1/11/13
to kern...@googlegroups.com, jus...@justindeanworsdale.com
Justin,

These are good questions. I will answer them according to the philosophies that I use in my own Kernel projects.

Regarding MVC 

I would not make a direct comparison as it is a different way of thinking. Definitely don't try to "fit" an MVC programming style into a Kernel project. The concept behind Kernel is very simple as Nicholas has probably discussed with you since it was patterned after his talk. Modules are generally single operating units. They can contain their own data or receive data through the events they listen to. You could also have data live in some namespaces on your hub if you want. But don't try to pigeon hole any one part of Kernel to any one part of MVC.

Globals

If by global you mean you need access to the variable from anywhere in the code then I would store it in the Kernel and provide a reference to it in the hub. This makes it easy to access from anywhere. Here is an example:

Kernel.extend(Kernel, {

  globals: { ... }

});

Kernel.extend(Kernel.hub.get('main'), {

  globals: Kernel.globals

});

Kernel.module.define('MyModule', {

  init: function() {
    // this.hub.globals
  }

});

Alternatively you can just store it on the hub which is accessible from Kernel as well as the modules. 

APIs

Apis that are meant to be used by modules should be accessible from the hub. The easiest way to do this is to define them as hub extensions. I usually keep hub extensions in their own files. Here is an example:

// hub.util.js
Kernel.extend(Kernel.hub.get('main'), {

  // namespace
  util: {

    myMethod: function() { ... }

  }

});

Then the module file would look like this:

// MyModule.js
Kernel.module.define('MyModule', {
  
  init: function() {

    this.hub.util.myMethod();

  }

});

Abstracting Base Libraries

Doing this requires a pretty good investment as there is a lot base libraries have to offer. So before you do so I would evaluate your need to swap out the base library at a future point in time. Weigh that need against the cost of abstracting the base library. I have not abstracted the base library on any of my projects so far, and I have used jQuery in all of them. In my case the need to replace jQuery with something else was simply non existent. 

If you do not abstract the base library you have the advantage of using any base library dependent widgets directly in your modules. This is very handy, but would of course be problematic if you did need to change the base library later. On the flip side, if you abstract the base library then you need to make custom widgets that use your abstraction. This is more work up front but does allow you to swap out the base library without touching the widgets.

And finally, yes, you would put the abstraction in Kernel and then reference it in the hub.

Hope this helps, let me know if you have any other questions.

Alan

justin worsdale

unread,
Jan 11, 2013, 6:28:01 PM1/11/13
to alindsay55661, kerneljs
Thanks for the thorough answers Alan! This is great.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages