Ansewr by @dlrust work, but it is not able to expand the parameter and pass from more than one place in the code. If you try to do something like this in your rendering template:
<script> define('config', function() { return { bootstrappedAccounts: <%= @accounts.to_json %>, bootstrappedProjects: <%= @projects.to_json(:collaborators => true) %> }; }); </script>
and in another file add some data
<script> define('config', function() { return { goods: <%= some data %>, showcaseList: <%= some json or array %> }; }); </script>
It has been overwritten ( DO NOT DISTRIBUT !!! ). In config there will be only the last declared data.
My solution: using the Backbone model with set / get data.
app.js
define("App", [], function() { window.App = { // Model: {}, // Collection: {}, // View: {}, // Router: {}, // Modal: {}, // UI UI: {} }; return window.App; });
global.js
define(["App", "underscore", "backbone"], function(App, _, Backbone) { "use strict";
index.php
<!DOCTYPE html> <html> <head> <script type="text/javascript" data-main="/app/init" src="/app/require/require.js"></script> </head> <body> <div id="tm-inner-wrap"> <div id="loader"><i class="uk-icon-refresh uk-icon-spin"></i></div> <?= $this->includeTpl('header_view'); ?> <div>your html content data</div> <?= $this->includeTpl('footer_view');?> <script> require(["global"], function(Global) { Global.set("notifyList", <?=json_encode($this->notifyList);?>); }); </script> </div> </body> </html>
another pattern
someTemplate.php
<div class="tm-inner-body"> <div class="uk-container uk-container-center"> // content data </div> </div> <script> require(["global", "module/index"], function(Global) { Global.set("goodList", <?=json_encode($this->goodList);?>); }); </script>
index.js
require(["App", "core", "jquery", "uikit!uikit-addons-min", "underscore", "backbone", "global", "module/good/goodView"], function(App, Core, $, UIkit, _, Backbone, Global, goodView) { "use strict"; // Global.get("notifyList"); its too able App.Collection.Good = new Backbone.Collection(Global.get("showcaseList")["items"]); // App.View.GoodList = Backbone.View.extend({ // el: ".tm-good-list", // init initialize: function() { this.collection = App.Collection.Good; // this.drawList(); }, // drawList: function() { this.$el.empty(); this.collection.each(function(item, index) { this.$el.append(this.drawItem(item)); }, this); }, // drawItem: function(data) { var good = new goodView({model: data}); return good.render().el; } }); App.View.Index = Backbone.View.extend({ el: "body", // events: { // }, // init initialize: function() { var $this = this; if(Global.get("showcaseList")) new App.View.GoodList(); } }); new App.View.Index(); });
File structure:
