What is the difference between $root and $parent?

Mou

I am learning KnockoutJS, but I do not understand the difference between $root and $parent usage. Please see this jsfiddle, or the below code:

<div data-bind="foreach:mainloop">
    $data Value: <span data-bind="text:$data.firstName"></span> 
                  <span data-bind="text:$data.lastName"></span> --(1)

    <br/>
    $parent Value: <span data-bind="text:firstName"> </span> 
                   <span data-bind="text:$parent.lastName"></span>
    <br/>
    $root Value: <span data-bind="text:firstName"></span>
                 <span data-bind="text:$root.lastName"></span>
    <br/>
        <hr/>
</div>
var mainLoopModel = function () {
    var self = this; // Root Level scope
    self.mainloop = ko.observableArray([{
        'firstName': 'jhon'
    }, {
        'firstName': 'sam'
    }]);
    self.lastName = ko.observable('peters');
    /*if you remove $data before lastName in note (1) you get undefined error because because mainloop dont have lastName root model has lastName so you have to access using parent or higher level */
}

ko.applyBindings(new mainLoopModel());

In the above code $root and $parent are both used for the same purpose: to refer outer scope variable. I just like to know is there any difference between the $root and $parent usages? If yes then please help me understand with a good example for correct usage.

Jeroen

They are similar but different:

  • $root refers to the view model applied to the DOM with ko.applyBindings;
  • $parent refers to the immediate outer scope;

Or, visually, from $data's perspective:

tree visualization

Or, in words of the relevant documentation:

  • $parent: This is the view model object in the parent context, the one immeditely outside the current context.

  • $root: This is the main view model object in the root context, i.e., the topmost parent context. It’s usually the object that was passed to ko.applyBindings. It is equivalent to $parents[$parents.length - 1].

  • $data: This is the view model object in the current context. In the root context, $data and $root are equivalent.

You'll only see a practical difference if you have view models nested more than one level, otherwise they will amount to the same thing.

It benefit is rather simple to demonstrate:

var Person = function(name) {
  var self = this;
  self.name = ko.observable(name);
  self.children = ko.observableArray([]);
}
  
var ViewModel = function() {
  var self = this;
  self.name = 'root view model';
  self.mainPerson = ko.observable();
}

var vm = new ViewModel(),
    grandpa = new Person('grandpa'),
    daddy = new Person('daddy'),
    son1 = new Person('marc'),
    son2 = new Person('john');

vm.mainPerson(grandpa);
grandpa.children.push(daddy);
daddy.children.push(son1);
daddy.children.push(son2);

ko.applyBindings(vm);
th, td { padding: 10px; border: 1px solid gray; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<script type="text/html" id="person">
  <tr>
    <td data-bind="text: $root.name"></td>
    <td data-bind="text: $parent.name"></td>
    <td data-bind="text: $data.name"></td>
  </tr>
  <!-- ko template: { name: 'person', foreach: children } --><!-- /ko -->
</script>

<table>
  <tr>
    <th>$root</th>
    <th>$parent</th>
    <th>$data</th>
  </tr>
  <!-- ko template: { name: 'person', data: mainPerson } --><!-- /ko -->
</table>

The $root is always the same. The $parent is different, depending on how deeply nested you are.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the difference between root and superuser?

From Dev

What is the difference between $scope.$root and $rootScope?

From Dev

What Is The Difference Between "root", "user", "group"?

From Dev

What is difference between root and base directory?

From Dev

What Is The Difference Between "root", "user", "group"?

From Dev

What is the difference between /root and /home in Kali linux

From Dev

What is the difference between a "/" (root) and a /home partition at installation?

From Dev

What is the difference between Owner and Parent of a control?

From Java

What is the difference between match_parent and fill_parent?

From Dev

what is the difference between <parent></parent>and<dependency><dependency> in maven?

From Dev

what is the difference between app-deployments and app-root on openshift?

From Dev

What is the difference between this and this.root inside a Polymer element

From Dev

What is the difference between adding a user to "sudoers" vs "root" group?

From Dev

What is the difference between sudo X and running X as root?

From Dev

What is the difference between the default user group and root group?

From Dev

Difference between / and /root in Ubuntu?

From Dev

Difference between / and /root in Ubuntu?

From Dev

What is the difference between tags parent, dependency and plugin (Maven)

From Java

What's the difference between extension use case and parent use case?

From Dev

what is the difference between target value blank,self, parent,top in HTML?

From Dev

What is the difference between owner and parent component in React.js

From Dev

What is the difference between parent.Bind and widget.Bind in wxPython

From Dev

What is the difference between spring parent context and child context?

From Dev

What's the difference between super() and Parent class name?

From Dev

What is the difference between tags parent, dependency and plugin (Maven)

From Dev

what is the difference in /# and ~# for a root user?

From Dev

What is the difference between `root ALL=(ALL:ALL) ALL` and `root ALL=(ALL) ALL`?

From Dev

What is the difference between the root user and the sudo command in Ubuntu? When should root be used?

From Dev

What is the difference between the group root and the group sudo? Why root user by default is not a member of the group sudo?

Related Related

  1. 1

    What is the difference between root and superuser?

  2. 2

    What is the difference between $scope.$root and $rootScope?

  3. 3

    What Is The Difference Between "root", "user", "group"?

  4. 4

    What is difference between root and base directory?

  5. 5

    What Is The Difference Between "root", "user", "group"?

  6. 6

    What is the difference between /root and /home in Kali linux

  7. 7

    What is the difference between a "/" (root) and a /home partition at installation?

  8. 8

    What is the difference between Owner and Parent of a control?

  9. 9

    What is the difference between match_parent and fill_parent?

  10. 10

    what is the difference between <parent></parent>and<dependency><dependency> in maven?

  11. 11

    what is the difference between app-deployments and app-root on openshift?

  12. 12

    What is the difference between this and this.root inside a Polymer element

  13. 13

    What is the difference between adding a user to "sudoers" vs "root" group?

  14. 14

    What is the difference between sudo X and running X as root?

  15. 15

    What is the difference between the default user group and root group?

  16. 16

    Difference between / and /root in Ubuntu?

  17. 17

    Difference between / and /root in Ubuntu?

  18. 18

    What is the difference between tags parent, dependency and plugin (Maven)

  19. 19

    What's the difference between extension use case and parent use case?

  20. 20

    what is the difference between target value blank,self, parent,top in HTML?

  21. 21

    What is the difference between owner and parent component in React.js

  22. 22

    What is the difference between parent.Bind and widget.Bind in wxPython

  23. 23

    What is the difference between spring parent context and child context?

  24. 24

    What's the difference between super() and Parent class name?

  25. 25

    What is the difference between tags parent, dependency and plugin (Maven)

  26. 26

    what is the difference in /# and ~# for a root user?

  27. 27

    What is the difference between `root ALL=(ALL:ALL) ALL` and `root ALL=(ALL) ALL`?

  28. 28

    What is the difference between the root user and the sudo command in Ubuntu? When should root be used?

  29. 29

    What is the difference between the group root and the group sudo? Why root user by default is not a member of the group sudo?

HotTag

Archive