views:

54

answers:

2

I have an object that hasn't been created yet (so it has no ID), but the user is in the process of filling out fields to create it. For one of the fields, they create an attribute for this object, but that attribute requires the object's ID.

That's where the issue is -- since the object hasn't actually been created yet (the user is in the process of filling out fields to create it), it doesn't have an ID. When you're editing the object and thus have the object's ID, creating attributes is no problem.

Here is the relationship:

OBJECT (name, id)
  ^ ATTRIBUTE (name, object_id, id)

How do you deal with situations like this? I was considering adding an "is_temporary" field to the object so that it can get an ID right off the bat, but surely there is a better way?

A: 

I think you need to make a distinction between an object being saved/persisted [in the database or wherever] and an object being created [instantiated in JS].

Are you saving the object to a database ? One method would be to pre-fetch the next id from the database. This has its own problems though (duplicates and all if you're page has a lot of traffic).

Can you save the object, get it's ID and then use it on the attribute ? If you want to avoid multiple trips to the server, then don't worry about id's until you submit everything and handle the id's server side.

A more concrete example would be nice too :-)

sjobe
+1  A: 

Great question, actually. You have two options:

  • Modify the "ATTRIBUTE" creation code to be able to handle creation and preservation through client-side form state.
  • Use the "is_temporary" field, and purge them regularly with a regular running task.

The latter is far easier to implement, but requires you to include is_temporary='false' everywhere else you access the objects, and also mucks up your ID sequences when people cancel.

The choice depends on the difficulty of the ATTRIBUTE creation code. If it's hard to make it persist in the form until the object is created (without an ID), then choose the latter option. Otherwise, choose the former.

razzed