Using same object for two different dom elements after fetching from the database.

24 views
Skip to first unread message

Martin Doktár

unread,
Feb 24, 2014, 3:52:22 PM2/24/14
to ang...@googlegroups.com
Hi.

I'm trying out angular for the first time and want to accomplish the following:

1. Multiple todo lists
2. Each todo can belong to one or more lists
3. Updating a todo belonging to more than one lists, updates the todo in all lists.
4. Store and load the lists/todos

I'm trying out different solutions on how to handle multiple lists/todos, but are running into some problems with storing and fetching the todos without loosing each todos singularity. 

For example:
2. Click "Add task to all lists" one or more times
3. Edit newly created task/todo and notice both lists updating
4. Click Save
5. Reload page and try to edit same tasks. Only one list is updated.

I think I understand why this happens, I just don't know enough about angularjs to fix it. I wan't the same behaviour before and after clicking save and reloading.

Any help is much appreciated!

Regards,
Martin

Sander Elias

unread,
Feb 25, 2014, 12:13:02 AM2/25/14
to ang...@googlegroups.com
Hi Martin,

This has nothing to do with AngularJS. When you create a new item, you create a single todo-object, you push this item into both lists.
Objects are always passed by reference, so you just have 1 item in 2 lists. Then you go and convert the thing to JSON. JSON is a text
format and has no idea of references.
When loading everything gets parsed into its own place from text, leaving you with seperate items.

Does this help you?
Regards
Sander

Luke Kende

unread,
Feb 25, 2014, 1:46:10 AM2/25/14
to ang...@googlegroups.com
Reiterating what Sander is saying, you are referencing one object initially in both arrays, but after parsing, you have two separate objects.  You'll need to manage this either upon saving or retrieving (or both) to get the intended results.

Martin Doktár

unread,
Feb 25, 2014, 3:42:52 AM2/25/14
to ang...@googlegroups.com
Hi! 

Thanks for your answers! I was guessing this was happening. Any tips on how I could manage this when retrieving the object? There are no "best practises" for this?

Regards,
Martin

Sander Elias

unread,
Feb 25, 2014, 4:02:01 AM2/25/14
to ang...@googlegroups.com

Hi Martin,

Simplest way, put an ID on every item. Your lists then become pointers to id’s. Hmm, sounds more difficult than it is.

var itemList1 = [];
var itemList2 = [];

var itemStorage = {};

item = {id:1, bla:blabla...}
itemStorage[item.id]=item
   // and so on!

Does that get you far enough? Don’t hesitate to ask!

Regards
Sander

Luke Kende

unread,
Feb 25, 2014, 4:05:53 AM2/25/14
to ang...@googlegroups.com
Nope.  Each of us programs a little differently.  Personally, I'd probably transform the data before sending to storage so that each todo is a single object that references the lists it belongs to.  That way upon retrieving them you just rebuild your lists' arrays pointing to a single object that shared again.



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/1vNeCii3cdk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages