Backbone-relational icon indicating copy to clipboard operation
Backbone-relational copied to clipboard

Prevent models from automatically being added to collections on instantiation

Open brian-mann opened this issue 12 years ago • 5 comments

When I instantiate models that belong to a parent relational model, they are automatically added to their particular collection - unlike the normal backbone behavior. While I would love for them to receive their natural "relational" attributes such as reverse relations from their parent model, I would prefer them not being immediately added to their collection until I choose for them to do so. Because of this behavior, there are several unwanted consequences that I'm experiencing.

Example:

I have a user model, and a posts model. User's have many posts. Typically users would click a button that takes them to a form where they would "create a new post". Posts are not created until the user fills out the required fields, and meets certain criteria. When the user was prompted with the form I would have normally instantiated the new Posts model in order for it to receive its natural default backbone attributes, amongst other things. At this point, I would also love for this model to automatically have its relational attributes set (such as user_id) based on the relations array. However because the natural behavior in Backbone Relational is to immediately add this model to its Posts collection (prior to it having any attributes from form fields applied to it), I have to work around a considerable amount of natural backbone events which trigger UI view logic (such as add events).

At this time however, the user hasn't necessarily filled out any data, and could have simply cancelled the form which would normally simply delete the model as per clean-up procedures. But in Backbone Relational, its already been added to the collection, and thus I have to do a ton of extra steps to ensure clean-up is handled properly. And this simple "opt-out" scenario doesn't even include any potential server side validation even after the user has submitted the form to ensure the model is validated prior to insertion into its collection.

In the end, it should be up to me when I decide this model legitimately belongs to its collection - and thus at that point I could now build off of the natural events triggered in doing so.

This example I gave was trite, but I have an application with literally over 150 models, and hundreds of scenarios of model and collection creation. Right now I'm working around this by instantiating every child relational model with its parents primary key attributes that don't match its reverse relation - and writing custom URL methods to pull off of these in the case when it is an isNew() model. It's a huge ordeal to continue doing this and essentially replicating the wonderful reverse relation capabilities Backbone Relational has built in. This project has made my application possible, and I sincerely appreciate it, but this one issue has really put a damper on things. Can we get this fixed?

There was a previous post about this before and you mentioned that you disagreed with the argument on the basis that the server should not block the UI. I agree with this, but there are numerous -- numerous times when you want to hold the model in limbo (regardless of server side validations) prior to its insertion into its collection. My example highlighted a few of those, but in my application it happens all of the time. Applications that are also based around finance and security would need server side validation in almost 100% of cases - so even if they didn't want to block UI's this would still not be possible. Sorry for the long write-up, I hope this has been helpful.

Much appreciated.

brian-mann avatar Apr 27 '12 06:04 brian-mann

I've run into this issue as well.

gorman avatar May 07 '12 04:05 gorman

What would be your proposed fix to this issue?

DouweM avatar May 16 '12 18:05 DouweM

A common approach would have newly-instantiated models to include a provisionalCollection link with the "official" relation updated following validation (client- or server-side).

shemol avatar Nov 05 '12 06:11 shemol

@brian-mann I understand your problem and I've run into it too, but I can't think of a good fix for this that would work in every common situation. If you have a suggestion, I'd love to hear it.

DouweM avatar Dec 20 '12 13:12 DouweM

@brian-mann I have the same problem. I've solved it by adding extra options when creating a new model and conditonal logic in the event handler. The extra options added in new Model() are sent to the options hash in the add event. Whit notes and users defined as you describe I do like this:

var userModel = new UserModel();
userModel.notes.on("add", function(model, options){
  if (options.userCreated) {
    note.showAsCreatedByUser()
  }
}, this);

var noteModel = new NoteModel({user: userModel},{userCreated:true});

ulmus avatar Mar 30 '13 09:03 ulmus