How to safely associate data to factory array while still seeing automatic updates from factory in Angular? - angularjs

Lets say I have an angular factory responsible for loading all Foos from the backend, and intermittently polls and updates foos. Everyone uses my FooFactory to reference Foo objects so they get updates when foo data changes.
In one directive I would like to associate some information with a foo object, lets say sorting information for what order someone wishes to list some child element of foo in a table.
At first glance it would be easy to set foo.sort="blah" on each foo object to store this data. However, this would change the foo objects shared by everyone, if someone in some other directive also did the same thing to store their table sorting data we would keep changing the others data, not good.
I could instead clone the foo object, then add my sort to the cloned object. However, now my foo is different then the one in the FooFactory. If FooFactory polls my backend and gets updated data my cloned foo will not see it, this is bad for me.
I could of course either keep a separate map of Foo objects to sort data, or some use of broadcast to keep my cloned foo objects up to date with fooFactory. This works, but takes some small effort to implement every where I want to associate data.
I'm wondering if angular already has a more convenient means of doing this. Is there some syntax that will allow me to 'clone' an object, so that data I add to the clone will not affect the original object, but still ensure the object is automatically updated when the original 'cloned' object is? basically to Alias all the existing data of an object but ensure new data appended to it is only appended to my copy of the object?

Related

how can i use core data to access an array saved in a different view controller?

I am saving an array into core data in one viewcontroller, how can i access that array of entities in a different view controller? also how would i apply the attributes (in this case they are string) to labels in the other view controller?
The data is stored in a database unrelated to any particular viewController, so that is no reason why it cannot be accessed from a different viewController. In general you should use a fetchedResultsController. A fetchedResutsController will do a fetch and then monitor core-data for changes. So if an a object is deleted or added the array of fetchedObjects will automatically update. This is a great feature, as accessing a core-data object that was deleted could lead to a crash.

Angular 1.5 correct way to synchronize changes in a component

I'm wondering what would be the best way to treat the following scenario:
I have a component (PeopleSelector) that loads objects of type Person and allows the user to select some of them. The data load logic (using a REST service) is proprietary of the component, so it should be private to it. The selected persons are included in an array of Person objects, included in a double bind.
In a container page, this component will be used, and the selected persons must be set (using an array of ids received from other service).
Problem is, the parent controller may receive the list of ids before the available persons are loaded in the child component (as services are asynchronous), so it cannot obtain the list of persons filtering the available objects in the child component.
So, how should I implement this use case?
Thanks

Backbone: Many small requests for model changes vs one collection sync?

What is a general good practice for when some action - changes multiple models in Backbone.js:
Trigger multiple PUT requests for each mode.save()
Single request to sync the entire collection
In case if the quantity of the changed models greater than 1 - definitely it should be the second item.
Usually, good REST api practice seems to suggest that you should update, save, create, delete single instances of persistent elements. In fact, you will find that a Backbone.Collection object does not implement these methods.
Also, if you use a standard URI scheme for your data access point, you will notice that a collection does not have a unique id.
GET /models //to get all items,
GET /models/:id //to read an element,
PUT /models/:id //to update an element,
POST /models/:id //to create an element,
DELETE /models/:id //to delete an element.
If you need to update every model of a collection on the server at once, maybe you need to ask why and there might be some re-thinking of the model structure. Maybe there should be a separate model holding that common information.
As suggested by Bart, you could implement a PATCH method to update only changed attributes of a particular element, thus saving bandwidth.
I like the first option, but I'd recommend you implement a PATCH behavior (only send updated attributes) to keep the requests as small as possible. This method gives you a more native "auto-save" feel like Google Docs. Of course, this all depends on your app and what you are doing.

ExtJS4 - Store per panel instance?

I'm really new to the MVC pattern in Ext.
I have a tabpanel with multiple instances of the same component (let's call it product), each should call the server when it's opened, with an id parameter.
Right now, in order to create these tabs - I use this in the Product controller
Which creates a new instance of a view, but I feel like it's really incorrect.
createMainView: function (opts) {
return Ext.widget("productDisplay", opts);
}
I call it from my "main" controller, like this:
var tab = this.application.getController("Products")
.createMainView({ productId : id, closable: true })
tabs.add(tab);
tabs.setActiveTab(tab);
What's the correct way to properly use multiple instances of a view, each having an instance of one store and behavior (via the controller).
Can I use one named store for them (with a js file under app/store/product.js)?
Should I manually call load on the store from the controller (to pass the productId), or is there a nicer way?
It's kind of very broad and interesting question which require big and thorough explanation (which you can find btw on the Sencha.com in their guides and manuals). I would like highlight couple points so you have something to start with:
Stores are usually global objects. You don't keep two instances of one store in general. You can use filtering (local or remote) if you need to present information from the that store in several different views. The only time you would need to clone store is if you want to present different information from that store in 2+ different views at the same time.
Controllers are usually spawned by main application object. You don't have to do anything special - just list them in the controllers: [] property. And then spawed when application is launched (before their views are created and rendered). Keep that in mind.
If you have modal view - it's ok to create it manually and either re-use it or destroy and re-create later. You can add filtering and loading to controller which creates these views. And you can re-use same view/controller objects for different tabs if you want.
If your views are presenting one instance of an object (like one product is displayed on each tab) - don't attach stores to those views. Just pass them individual model (record).
I would recommend creating the stores that relate only to that view instance inside of the view's initComponent method.
Your controller's control handlers should be coded in a way that they can differentiate which view dispatched the event. This should not be too difficult because almost all view events contain a reference to the component which fired the event. You could then use the relative query selectors, e.g.: myEventFiringComponent.up('anotherComponent') or myEventFiringComponent.down('anotherComponent') to get a handle on a different component in the same view if you need to.
Please see this post for a full explanation.

When using an object database, how do you handle significant changes to your object model?

If you use an object database, what happens when you need to change the structure of your object model?
For instance, I'm playing around with the Google App Engine. While I'm developing my app, I've realized that in some cases, I mis-named a class, and I want to change the name. And I have two classes that I think I need to consolidate.
However,I don't think I can, because the name of the class in intuitively tied into the datastore, and there is actual data stored under those class names.
I suppose the good thing about the "old way" of abstracting the object model from the data storage is that the data storage doesn't know anything about the object model --it's just data. So, you can change your object model and just load the data out of the datastore differently.
So, in general, when using a datastore which is intimate with your data model...how do you change things around?
If it's just class naming you're concerned about, you can change the class name without changing the kind (the identifier that is used in the datastore):
class Foo(db.Model):
#classmethod
def kind(cls):
return 'Bar'
If you want to rename your class, just implement the kind() method as above, and have it return the old kind name.
If you need to make changes to the actual representation of data in the datastore, you'll have to run a mapreduce to update the old data.
The same way you do it in relational databases, except without a nice simple SQL script: http://code.google.com/appengine/articles/update_schema.html
Also, just like the old days, objects without properties don't automatically get defaults and properties that don't exist in the schema still hang around as phantoms in the objects.
To rename a property, I expect you can remove the old property (the phantom hangs around) add the new name, populate the data with a copy from the old (phantom) property. The re-written object will only have the new property
You may be able to do it the way we are doing it in our project:
Before we update the object-model (schema), we export our data to a file or blob in json format using a custom export function and version tag on top. After the schema has been updated we import the json with another custom function which creates new entities and populates them with old data. Of course the import version needs to know the json format associated with each version number.

Resources