Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think just using Backbone.js is perfect for this type of application you are describing. You have probably already read this, but most of the backbone literature is focused around your views having associated server generated JSON models and collections, then using the View's render function to generate (on the client) the HTML UI that represents the model/collection.</p> <p>However it doesn't <strong>have</strong> to be used this way. In fact there is nothing stopping you attaching views to existing elements that contain content already, which gives you all of the benefits of Backbone's modularity, events system and so on. I often use views that have no model or collection, purely because I like the conformity of style. I have also used an approach like I describe below in the cases where I have had to work with older, existing applications that have not yet got, or never will have a nice REST API, but they do provide content in HTML.</p> <p>Firstly, lets assume the following HTML represents one of your widgets:</p> <pre><code>&lt;div id="widget"&gt; &lt;div class="widget-title"&gt;&lt;/div&gt; &lt;div class="widget-body"&gt; &lt;!-- assume lots more html is in here --&gt; &lt;a href="/Controller/DoWidgetStuff"&gt;Do something!&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>In this case, you could use backbone with a single <code>Widget</code> Model. This would be a very simple model, like this:</p> <pre><code>App.WidgetModel = Backbone.Model.extend({ intialize: function () { this.url = this.options.url; } }); </code></pre> <p>Take note of the fact the Widget receives a URL as a parameter to its constructor/initialize function. This widget model would represent many of your widgets (and of course you could adopt this general approach with more complicated models and pluck different data from the rendered HTML). So next for your views. As you probably know, normally you pass most views a model or collection when you instantiate them. However in this case, you could create the Widget model in your View's initialize Function and pass it a URL from the pre-rendered HTML as follows:</p> <pre><code>App.WidgetView = App.View.ComboboxView = Backbone.View.extend({ initialize: function () { this.model = new App.WidgetModel({}, { url: this.$("a").attr("href") }); } // rest of the view code }); </code></pre> <p>So instantiating the view would be something like:</p> <pre><code>new App.WidgetView({el: $("#widget")})' </code></pre> <p>By doing all of the above you can do pretty much everything else that backbone offers you and its modular and encapsulated nicely, which is what you are after.</p> <p>The end result of this whole approach is:</p> <ol> <li>You have rendered the Widget UI as pure HTML which (I assume) is functional without JavaScript.</li> <li>You attach a View to the existing HTML.</li> <li>You pass into the View as options, content by extracted (such as a URL) from the rendered HTML with jQuery.</li> <li>The View is responsible for instantiating the Model passing on the relevant options the model needs (such as a URL).</li> <li>This means all dynamic server side content is intially contained in the rendered HTML and your View is a modular JavaScript component that can do stuff to it, which I think is the end result you're after.</li> </ol> <p>So you mentioned that you would like to have AJAX functionality for your widgets and that fine with this approach too. Using this approach, you can now use the standard Backbone <code>fetch</code> and <code>save</code> functions on the <code>Widget</code> model to get new content. In this example it is from the URL retrieved from the rendered HTML. When you get the response, you can use the view's, render function, or other finer grained functions to update the HTML on the page as required. </p> <p><strong>A few points:</strong></p> <p>The only thing to look out for is that you'll need to change the content type of the <code>fetch</code> and <code>save</code> functions to "text/html" if that's what the server is providing. For example:</p> <pre><code>this.model.fetch({ type: "POST", contentType: "text/html" }); </code></pre> <p>Lastly, the model I have proposed is instantiated with no content. However if your ajax calls are a content type of "text/html", you may need to play around with you model so it can store this content in its attributes collection properly. See <a href="https://stackoverflow.com/a/12332329/1439720">this answer</a> for more information. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload