What's the best way to use JSON?

For now, I write my source code like this:
// ok.js
var ok= {
}

// blocks.js
ok.blocks = [
  // blocks definitions.
]

// Ok.Blocks.js
enyo.kind({
	name: "Ok.Panorama.Blocks",
	components: ok.blocks
});
Now, I want to separate the blocks definitions to a JSON file. Like this:
// blocks.json
[
  // blocks definitions.
]

// ok.js
var ok= {
  blocks: "blocks.json" // First question: What's the best way to load the json file here?
}
Second question: I want to hear your advice about how to organize the source code.

Comments

  • To load a JSON file (or any file), you'll need to enyo.Ajax to fetch the code. Check out the Ajax sample in the sampler for an example.

    That said, I wouldn't recommend externalizing just the component block alone into another file. I think you'll find this more cumbersome to work with as you jump back and forth to identify what you've named a control in order to script it. You do have the right idea to minimize the code around the view layer. I'd definitely suggest limiting the JS in the view layer to view population and event handlers and move data and business logic elsewhere (services, utility kinds, controllers, or others based on your app architecture preference).
Sign In or Register to comment.