Detaching dynamics $scope - angularjs

Good day to all.
There is a problem.
Create one object $rootScope.metaContent (need just $rootScope). In the future, to work with the data of the object, creating a new based on the first $scope.addMeta = $rootScope.metaContent. Needless to say, when I'm working with the second (adding an object, change its data or purify) is changed first. But I need to work with the second ($scope.addMeta), that would be the first not changed. It is necessary to restore the data to the source by pressing one button "Undoing".
How can they decouple from each other???
Thanks in advance!!!

You can create a deep copy of an object or an array in angular using angular.copy
$scope.addMeta = angular.copy($rootScope.metaContent);

Related

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

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?

Backbone - Reusing Views vs Creating new Views

Writing my first Backbone app - came across a predicament wherein i am unable to choose which is the best way to move forward.
Scenario : User clicks an edit button , a new view is loaded . Approach is as below.
renderEditView: function(){
if(my.namespace.view){
my.namespace.view.render();
}else{
my.namespace.view= new editView({model:my.namespace.model});
}
}
Basically, i am assigning my view to a namespaced variable and resuing it as required. Didn't face any problems as such.
But some advocate recreating the View again using new editView({model:xxx}); whenever the edit button is clicked . i Would like to know which one is the better practice and why?
P.S: i am aware of the 'event ghosting' problem in BB apps, and the excellent solution provided by Derick Bailey .But still would love to know the pros and cons between the approaches.
This is indeed an opinion, because either way will work if you (as you mention) take care of cleaning up previous views if you decide to instantiate a new one every time you want to re-render. It's important to avoid duplicating lingering events from every instance that you want to replace by creating a new one.
Personally I have used both strategies and never had problems with them so far.
When re-using a view, I bind the view as a property to the controller object that renders the view, pretty much the same way you do it.
Theoretically, I don't see a reason to re-instantiate a view if it was already created before. It isn't that you really require a new instance, it's just that you want to re-render it.
Sidenote
For re-rendering views, Backbone Marionette offers regions, which are convenience objects that allow you to do things like:
var myView = new MyView();
var region = new Marionette.Region({el: "#container"});
region.show(myView);
In case you would decide to instantiate a new view every time, these regions take care that previously rendered views are properly cleaned up.

Delphi XE3: DBLookupCombo Dropdown side effect

We are porting a D6 application to XE3.
In D6 I inherited a complex code which used shared datasets and datasources everywhere.
This worked well in D6.
After we could run the XE3 version, we experienced that lookup combo boxes changed.
On dropdown they reset the other dropdown's keyvalues (everywhere in the program)!
If two dropdowns use on dataset, and if I click on the first to down it, and select, on down the second keyvalue changed to NULL; and reverse - if I click on the second, the first's keyvalue change to NULL...
This is global in this program, so I need to find fast solution.
May somebody have any information about this "bug" (or "feature"? :-) ), or have a solution in his/her hand?
Thanks for any answer!
This is intentional. Take a look at the implemention of TCustomDBLookupComboBox.ListLinkDataChanged; in Vcl.DBCtrls. You will find the comment:
{ Fix for Defect# 204311, Non-bound control should clear when list datasource changes }
Solution: put your datasets on a data module. Instantiate that for every form, so every form works with a separate instance of the dataset. Make sure you set the name of the data module to an empty string after instantiation, or the Delphi streaming system will still use the first correctly named instance when hooking up the form's datasources with the datasets.
When the data module(s) are in the form's uses clause (interface or implementation doesn't matter) the IDE will still offer you their components through the Object Inspector.
You will want to put the database connection on a different data module that you only instantiate once (possibly automatically).

Record is losing its store

I'm creating a class with extjs record in config. I keep a reference to this record. On creating the class instance I expected the record and It's completely ok.
Later, in one of the object methods I'm trying to use this record. Quite unexpectedly the record has lost its store reference. Except this the record looks ok with the right data.
I hacked the situation keeping reference to the store in initialization phase:
this.store = this.recordLoosingItsStore.store;
And later I can retrieve my record like this:
var recordOK = this.store.getById( this.recordLoosingItsStore.getId() );
Can anyone shares opinion what's happen and where my record.store disappears? How can I debug it easily? I tried to advice for store datachanged event but this didn't produce any positive results.
If you check the documentation, it states that a record instance may only belong to a store instance at a time. Is it possible that you are assigning it to a different store at any point in your code and then destroying that other store? If that is the case, using the .copy method when assigning the record to a different store should solve the issue.
More info: http://docs.sencha.com/ext-js/3-4/#!/api/Ext.data.Record
It's hard to say where it's loosing it without looking at your code. Try to post some code here. If it's too big - try to extract places where you first load record and then all places where you do something with the store/record.

nHibernate: Reset an object's original state

I have a very basic query. I am using WPF Binding to edit a object which is loaded by a ISession. If somebody edits this object in the form, because of two way binding and a stateful session, whenever I close the session, changes to the object made in the form are stored back in the database. Which is the best way to avoid this?
The methods I know:
Shadow copy the object and use the copied object as the DataContext (the method I am using as of now).
ISession.Clear
Use IStatelessSession.
Is there any way to reset the object to it's original form before closing the ISession?
If you look here: http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate.aspx
It is an example of finding dirty properties. NHibernate internally tracks a persistent object's state by way of the EntityEntry object.
This is useful for you, because with a little modification to the method above, you're able to get old values back ... which you can use to reset the properties.
As for closing your session causing the object to be flushed to the database, you can set the session FlushMode to FlushMode.Never. This will mean no database sync occurs until you call Session.Flush().
Alternatively, you can hook into IFlushEntityEventListener to reset the object state. There are a reasonable examples of using the NHibernate event system on google.
See Managing the caches on NHibernate Forge:
When Flush() is subsequently called, the state of that object will be synchronized with the database. If you do not want this synchronization to occur or if you are processing a huge number of objects and need to manage memory efficiently, the Evict() method may be used to remove the object and its collections from the first-level cache.
I think that sounds like what you want.
I would suggest the use of transactions. You just rollbackthe transaction if that is the case? what do you think?

Resources