AngularJS Directives: ngModelCtrl.$parsers - javascript

After looking at a few different custom directives, I have noticed that some people use ngModelCtrl.$parsers.push() and some others use ngModel.$parsers.unshift(). Could someone explain the difference.

push() adds to the end of an array.
unshift() places the new element at the front of the array.
The order matters because parsers are executed in order. From the Angular docs:
Array of functions to execute, as a pipeline, whenever the control reads value from the DOM. The functions are called in array order, each passing its return value through to the next. The last return value is forwarded to the $validators collection.

$parsers.unshift() put your parser function in the beginning of the list, so it will be executed before all the other, $parsers.push() put it in the end.
$parsers it is an array object and provides array functions

Related

What's the difference between these two functions

So i have an json array named users. Im trying to update an individual field if id comes from request equals to users id. When i use for loop it works but it doesn't when i try to use forEach (there are no errors). I don't understand the difference between these two.
In the forEach, user is just a variable that happens to start with the value passed in as a parameter to your lambda function. Setting user = ... doesn't actually change anything in the original array. Nor does it change the properties on the object that is currently in that array.
Consider using Object.assign() instead:
Object.assign(user, req.body);

Riak MapReduce in single node using javascript and python

I want to perform MapReduce job on data in Riak DB using javascript. But stuck in very begining, i couldnot understand how it is returning value.
client = riak.RiakClient()
query = client.add('user')
query.map("""
function(v){
var i=0;
i++;
return [i];
}
""")
for result in query.run():
print "%s" % (result);
For simplicity i have checked the above example.
Here query is bucket and user contain five sets of data in RiakDB.
i think map() returns single value but it returns array with 5 value, i think equivalent to five set of data in RiakDB.
1
1
1
1
1
And here, why I can return only array? it treats each dataset independently, and returns for each. so i think i have five 1's. Due to this reason when i process fetched data inside map(), returns gives unexpected result for me.
so please give me some suggestion. i think it is basic thing but i couldnot get it. i highly appreciate your help.
When you run a MapReduce job, the map phase code is sent out to the vnodes where the data is stored and executed for each value in the data. The resulting arrays are collected and passed to a single reduce phase, which also returns an array. If there are sufficiently many results, the reduce phase may be run multiple times, with the previous reduce result and a batch of map results as input.
The fact that you are getting 5 results implies that 5 keys were seen in your bucket. There is no global state shared between instances of the map phase function, so each will have an independent i, which is why each result is 1.
You might try returning [v.key] so that you have something unique for each one, or if the values are expected to be small, you could return [JSON.stringify(v)] so you can see the entire structure that is passed to the map.
You should note that according to the docs site javascript Map Reduce has been officially deprecated, so you may want to use Erlang functions for new development.

.config() of AngularJS

I am new in AngularJS. I am learning AngularJS. In this regard I found below syntax in a tutorial.
app.config(['$routeProvider',function($routeProvider){}]);
Here .config() is a method. I am passing an array as parameter of this .config() method. '$routeProvider' and a anonymous function are two elements of that array. Am I right ??
Can I make any change of the first element of the array, like '$routeProvid' or '$routeProvideraa' or '$outeProvider'??
Can I make any change of the second element of the array, like function($routeProv) or function($routeProviraa) or function($outeProvi)??
Thanks
UPDATE
I think I failed to express. I am updating the question
Can I use below syntax??
app.config(['$routeProvid',function($routeProvider){}]);
or
app.config(['$routeProvider',function($routeProv){}]);
or
app.config(['$teProvider',function($routeProvi){}]);
Did you get this portion ??
.config() is a method. I am passing an array as parameter to this .config() method. '$routeProvider' and a anonymous function are two elements of that array.
AngularJS uses the parameter name for dependency injection; once minified the variant without the array would break.
Since strings aren't modified during minification they are used to map to their parameters. So to answer your question if you use the inline array annotation you can change the arguments to whatever you want. If you don't rename the strings or change the order of the dependencies you should be fine.
this is the annotation in AngularJS...
Basically, AngularJS provides a very useful Dependency Injection system that allows to retrieve dependencies simply declaring them as function parameters...
So, if you register something through the angular recipes, then, you are able to retrieve them in this way:
angular
.module('test', [])
.value('myValue', 'Hello World')
;
angular
.module('test')
.run(function(myValue) {
console.log(myValue);
})
;
This is very useful because allows scalability, testability, ecc... but creates one problem: It cannot be minified
The $injector cannot understand the result of the minification, because function parameters are mangled and the above code becomes:
angular.module('test').run(function(a) {console.log(a);})
So, the only way to minify angularjs code is to annotate each dependency that must be injected.
Annotation can be made in two ways:
via $inject property
using the array-like annotation
Your code belong to the second case, basically, instead of passing the function directly, you can pass a javascript array that has a function in the last index position:
[function() {}]
the above example becomes:
angular.module('test').run(['myValue', function(a) {console.log(a);}]);
There are many build tools that perform automatic annotation (ng-annotate), and is always suggested run the angularjs in strict-di mode.
Hope it helps
The first n elements of the array are factory names and you should use something that is registered at this point. Here - router - which has its own provider (the thing that can setup the router service). The config itself hasn't got too many options to choose from as it is parsed before your app is instantinated, hence you'll use providers and constants here. https://docs.angularjs.org/guide/providers
The last element of the array is the function whose arguments should correspond to the things you pass in the array. The names here are your choice, although keep in mind the order.
So, summing up, you can't rename the thingies in the quotation marks '$routeProvider', instead, know your app and what you are installing. But you can use your own names for them in the latter function($mynameforrouteprovider) but you have to keep them in order. Best if you use the same names that their author uses.

How to autorender a JSON array using pure.js?

I am trying to use the pure.js templating engine to convert a JSON array into HTML code.
I understand how to use autoRender() to map an associative map to HTML: http://jsfiddle.net/P7H98/
but if I replace the associative map with an array, I end up with iteration that inserts empty rows: http://jsfiddle.net/P7H98/1/
Is it possible to autoRender() an array and end up with the same output as the first example?
UPDATE: Nesting <div class="toString"></div> inside the <li> node fixes the problem. But I'm still not sure why. Surely there is a more readable solution to this problem?
The toString is a coincidence.
The property names triggers a loop, because it points to an array.
Then the toString is a method that exists for each array element, and is called.
If you add the class toString on the LI, it will work.
You should try using render and directives.
They can be generated on the fly, and give you a more precise control of what needs to be done.

How correct use terelik getGridMasterTableView().filter() methot on javascript on client side?

I use telerik radGriD funcrion on javascript client-side:
getGridMasterTableView().get_filterExpressions().clear(); - clear array of filter expression.
getGridMasterTableView().filter(fieldName, strData, GetExpressionValue(fieldExpression)); - add elenent in array filter expression.
when I call this getGridMasterTableView().filter(fieldName, strData, GetExpressionValue(fieldExpression)) method two times, when fieldName allways the same, the method getGridMasterTableView().get_filterExpressions() in watch shows that it simply rewrites element but I need that it add new element. How resolve it?
Are you attempting to apply two filters on the same column? From the sound of things you are filtering once, and then trying to filter that same result once more. You can only have one filter on a column at a time, so when you call the filter() method the second time it just removes the old filter and filters according to the last filter() call.
For more information regarding client-side filtering I recommend looking over this documentation article.

Categories

Resources