ethanw’s picture

Development on this has begun, involving a refactoring of the drupalbackbone.js file into one file containing classes common to both RestWS and Services flavors and another with objects that are backend specific.

Due to a difference in HTTP verb implementation in the REST approach of Backbone vs. RestWS, however, further work on this task will require the resolution of #1472634: HTTP PUT / POST Reversed for CRUD CREATE / UPDATE Operations.

ethanw’s picture

Status: Active » Postponed

I'm marking this as postponed till the PUT/POST issue for RESTws is resolved.

ethanw’s picture

Status: Postponed » Active

RestWS addresses the HTTP verb issue, this can now be taken back up.

ethanw’s picture

Basic RestWS support has been added to dev. This involved a significant refactor. All backend-specific code is now provided via additional models, with `hook_backbone_backends` used to indicate a module provides a backend.

A few other notes:

1. The RestWS-specific JS is much more minimal than the Services version, owing mainly to RestWS being designed to be much more similar to canonical REST implementations.
2. Also added is a backbone_restws_examples module. which has the examples from the DrupalCon Munich presentation adapted for RestWS.
3. I finally figured out why some properties were not being inherited multiple levels in the hierarchy, basically non-Backbone-standard properties that are not methods can't use the standard extend({prop:va}) syntax. See [this post to the Backbone.js Google group](!topic/backbonejs/463jkNR5dW4) for (hopefully) discussion on this.
4. You've got to specify RestWS in the Backbone admin page for the examples to work.

ethanw’s picture

Status: Active » Needs review
floptikal’s picture

Fails when site in subfolder. Patch adds Drupal.settings.basePath.substr(1) to rooturl

frega’s picture

4.37 KB

I ran into a similar issue like @floptikal. Didn't see this issue until right now ... so some of this might now apply.

- I think it might be better to have custom "url"-method in the Drupal.Backbone.Models.Entity that to adjust the urlRoot-Property.
- - lacked a dependency on backbone_restws and unnecessarily declared a .module file

- Specifying RestWS in the Backbone admin page did not work for me instead I fixed the drupal_add_library-calls to use backbone_restws instead of backbone - but i might be missing something here, because i could see the corresponding drupal-variable 'backbone_backend_module' being used at all.

Please find a patch attached.

ethanw’s picture

+1 for the custom URL idea, very flexible. Could be used on XS or non-Drupal-root-based Backbone apps as well. Nice idea.

frega’s picture

@ethanw - can you paste / commit the view that's referenced in the backbone_restws_examples?

ethanw’s picture

No view should be used. Should just be using restws query params.

frega’s picture

sorry, my bad - based on irc i was assuming that there was a views-view for the collections, but i see it's restws' entity listing (which are working in my installation :)

frega’s picture

ok, i found out why the collections weren't working; newest restws-7.x-2.0-alpha2 is required (alpha1 doesn't support the .{FORMAT} notation afaics).

frega’s picture

I've got everything working now, looks lovely. Would you like me to write a patch, that does a little sanity checking, i.e.
1) make sure the version of restws is uptodate
2) find some nid instead of hard-coding nid 2 and warn if there aren't any nodes at all?
3) check that there is a node bundle "page" and that there are nodes of the bundle instantiated?

ethanw’s picture

Absolutely. The current examples were straight from the presentation and do now edge case checking, etc.

frega’s picture

Updated the restws example. Fixed drupal_add_library-calls. Removed hard-coded nids/node types. Added a few explanatory messages. A few whitespace issues.
Will file a separate patch for the hook_requirements-implementation for backbone_restws.module.

frega’s picture

Sorry had to reroll, because I forgot to include url()-patch for modules/backbone_restws/js/drupalbackbone-restws.js.

frega’s picture

Rerolled patch to match changes in commit a92078e. Also change dependency[] in from restws to backbone_restws. This patch is kinda required for the backbone_restws_examples to work, so i'd be great if this could get reviewed quickly :)

frega’s picture

can we commit this? it'd be nice to have working examples in the -dev branch :)

ps. if #1791186: backbone_example include backbone_services' drupalbackbone-services library gets committed the examples for both backbends should be working again ...

ethanw’s picture

Thanks for the repeated pings, frega.

It looks like /backbone_restws_examples/node_event_bind isn't working with this patch, at least for me. It seems to not be filtering out the "read-only" attributes.

I found this was due to calling the initializer via __super__ when initially developing the RestWS piece, so perhaps that's what's happening here?

I'll be able to dig in more in a bit, but to start: are you seeing similar behavior?

frega’s picture

Hi Ethan,
sorry, can't seem to reproduce this behaviour (Chrome 22.0.1229.79, and FF 15); can you reset your repo to the latest git/7.x-1.x, disable and uninstall all modules and then re-enable backbone_restws_examples with the patch above applied? I had some issues JS issues when both enabling backbone_services + backbone_restws.
Best, Fredrik

atlea’s picture

Quick review of the patch:

- collection_render is not using the node type as query, just plain fetch.
- settings should probably be passed to the behavior?

Here a patch for collection_render.js. Inline.. apologies for being so lazy. The changes to how settings is read should be made to the other behaviors as well.


--- <html>collection_render.js (<b>Today 1:52:26 AM</b>)</html>
+++ <html><b>Current File</b></html>
@@ -1,6 +1,7 @@
 (function ($){
   Drupal.behaviors.backbone_restws_examples = {
-    attach: function() {
+    attach: function(context, settings) {
       // Create Node Model class
       // Create Collection class
       var collection = new Drupal.Backbone.Collections.RestWS.NodeIndex();
@@ -27,10 +28,8 @@
       // Fetch collection
-      collection.fetch({
-        success: function(collection) {
-          // this is empty, since event handlers take care of everything!
-        }
+      collection.fetchQuery({
+        type: settings.backbone_restws_examples.type
atlea’s picture

..and a note: RestWS seems to be failing when trying to update a node that contains an image. I have created an issue over at #1819594: PUT/update fails with 406 on entity/node that contains an image/file

frega’s picture

Rerolled patch as per #21. Indeed prettier and better.

Note: because we don't wrap the instantiation of Backbone views/models/collections in a "once()" every Drupal.ajax-call will reinstantiate and run the behaviour again. This should probably be taken care of at some point :)

atlea’s picture

The ajax could have changed the node, so loading and rendering it again might not be wrong.. one could check if the element is in the context to be refreshed or not..

e.g. wrap it in

if ($("#backbone-restws-examples-node-render-app", context).length > 0) {
<view is in context, so (re)run code>

Now, if i open up Console and type Drupal.attachBehaviors(); it should run (as document is the default context), but not if I type e.g. Drupal.attachBehaviors('.breadcrumb');

..but we must take care to not make this first example too complex!

Perhaps even shorten it down to:

(function ($) {
  Drupal.behaviors.backbone_restws_examples = {
    attach: function(context, settings) {
      // Load a node
      var myNode = new Drupal.Backbone.Models.Node({
        nid: settings.backbone_restws_examples.nid

      // create our view instance
      var myNodeView = new Drupal.Backbone.Views.Base({

      // fetch the node, render when we've fetched
      // (better way to do this is with bind(change))
        success: function() {
frega’s picture

Status: Needs review » Fixed

Applied patch from #23 in commit 7295703.

@atlea - I have set this issue to fixed (as the initial restws-version and the accompanying example work), and move the larger "architectural questions" you raise (interaction w/ Drupal.ajax/.behaviors, how to encapsulate "apps") to a separate issue, is that ok w/ you?

atlea’s picture

Sure! On that note, I believe it is wrong to attach drupalbackbone.js to the page/using behaviors for library functions that don't interact with the page. But we can continue that discussion when you create a new issue. ;) - Atle

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.