Executing functions when getting and updating data from a model

145 views
Skip to first unread message

Hemure

unread,
Sep 18, 2012, 3:47:13 AM9/18/12
to ang...@googlegroups.com
Hi

I'm new to AngularJS and working on my first project - and I must say, this framework seems very promising.

My scenario:
I've got data loaded of the server and into a model in the client.
Now I want to do things with the data, and have run into 2 use cases that I'm not sure how to handle.

1. Formating data when displaying
Is there any way to manipulate data, like e.g {{PhoneFormat(model.phoneNumber)}} or some other syntax?

2.Do something on data change
I plan not to update data on the server whenever something changes, but rather do bulk-updates when the users is done editing the dataset.
For that reason I've included a dataChanged property in the model, and would like to have that set=true when ever a value in the record/item changes.
I think the concept of watches is what I need, but ...


Thank you for any clues you can provide,
/Henrik

Peter Bacon Darwin

unread,
Sep 18, 2012, 4:40:06 AM9/18/12
to ang...@googlegroups.com
1. Try using filters:  http://docs.angularjs.org/guide/dev_guide.templates.filters 
2. Something like this:
$scope.$watch('mydataobject', function() { $scope.dataChanged = true; }, true);
The last parameter true tells the watch to watch for changes to any field in the object that is being watched.

Pete


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

thomas.l...@gmail.com

unread,
Sep 19, 2012, 5:53:21 AM9/19/12
to ang...@googlegroups.com
If he has many data the watch with "true" will take so longer that is app will not respond for seconds. I've tried with an array of 12000 objects and it's no usable.

James Wright

unread,
Sep 19, 2012, 7:41:49 AM9/19/12
to ang...@googlegroups.com, thomas.l...@gmail.com
i think if you have that many objects in memory time to keep some of it on the server and only shuffle it out in 'pages'

working with android 2.2 phones we ran into some javascript bottlenecks with fewer objects than that, so we worked off a smaller dataset as required and it was fine.

there is also the idea that it doesn't have to be instant, throw a spinner up while it is processing. folks are used to it from ajax websites anyway.

Henrik Munk Molke Rediin

unread,
Sep 19, 2012, 7:47:07 AM9/19/12
to ang...@googlegroups.com
HI

Thank you all for the answers.
I'll go with the later suggestion, keeping my loaded data set as small as possible.

/Henrik

thomas.l...@gmail.com

unread,
Sep 19, 2012, 8:15:49 AM9/19/12
to ang...@googlegroups.com
For my usecase, i can't have a small part of it, i use this data to feed a treeview and a treeview can't be paginate.. So 12000 item are required and i can't use watch on it to handle added/removed item, for the moment i don't have a solution.

James Wright

unread,
Sep 19, 2012, 8:32:46 AM9/19/12
to ang...@googlegroups.com, thomas.l...@gmail.com
a common technique when building treeviews in ajax is to only fetch the data for the node that is expanded.

so you might only fetch a branch instead of the entire tree.

Godmar Back

unread,
Sep 19, 2012, 9:11:42 AM9/19/12
to ang...@googlegroups.com

If you don't need to know which model item changed, just if any changed, then use a $watch without expression. This then will fire only twice, not 12,000 times.  Oh, and if that worked for you, there's another use case for my pet peeve at https://github.com/angular/angular.js/issues/1305

 - Godmar

Josh Kurz

unread,
Sep 19, 2012, 2:28:11 PM9/19/12
to ang...@googlegroups.com
Try to watch the length of the array instead of all the objects if u just need to worry about addition and subtraction of objects. 

Josh Kurz 

Reply all
Reply to author
Forward
0 new messages