Thibaud Clement
Thibaud Clement

Reputation: 6907

Meteor: what is "data context"?

Just starting with Meteor, and going through the Meteor Tutorial by Matthew Platts.

In this tutorial, as well as in the official Meteor Documentation, there are many references to the concept of data context, but I can't seem to find a cristal clear definition / explanation (with examples) of what this is.

For instance, in the 2.4.3 Rendering Data with Helpers section, we read:

Notice that inside of the #each block we go {{name}}, even though we have no name helper defined. This is because the data context changes inside the #each block. We loop through each item in the teams array, and since each item has a “name” attribute Meteor will automatically create a {{ name }} helper.

UPDATE: Actually, just reading through the end of this very section, the author recommends a resource that makes things pretty clear: A Guide to Meteor Templates & Data Contexts. Still no accurate definition though.

So, in Meteor, what is data context exactly?

Upvotes: 0

Views: 513

Answers (3)

Michel Floyd
Michel Floyd

Reputation: 20256

A data context can be one of 3 things: (unless I've missed some)

  1. A cursor, i.e. the result of a Collection.find()
  2. An array of objects, i.e. just some array or the result of a Collection.find().fetch()
  3. An individual object, i.e. { _id: "123", name: "Orthoclase E. Feldspar" }

{{#each foo}} loops over a cursor or array context and changes the context to an individual object. {{#with bar}} just says which helper to use (in this case bar) to set the data context.

During development, but especially while learning Meteor, it helps to have console.log(this) at the top of your helper code just to double check what the data context is. It is this.

Upvotes: 0

Sasikanth
Sasikanth

Reputation: 3043

I'll try to explain as much as I know, correct me if I'm wrong.

I'll explain using following snippet:

<template name="posts">
  {{#each posts}}
    <p>{{name}}</p>
  {{/each}}
</template>

Let's assume it will display all the posts names from a blog:

First Post
Second post
Third post
..........
..........

I assume you know the concept of helpers and events.

In the above snippet, in general for {{name}}, meteor searches for the helper called name in helpers:

Template.posts.helpers({
  name: function(){
   return "dummy text";
  }
});

If it finds any, it runs that helpers and displays the value.

So here, it outputs:

dummy text
dummy text
dummy text
...........

But if it doesn't find any helpers, it will search in data context.

Let's assume for posts we're returning some data:

   Template.posts.helpers({
      posts: function(){
       return Posts.find().fetch();
      }
    });

The data we're sending to posts helper looks like this:

{name: "First post", _id: "xxx", ......},
{name: "Second post", _id: "yyy", ......}
{name: "Third post", _id: "zzz", ......}
.................

In the code for {{#each posts}}, it loops through every object and displays name property("First Post","Second Post,"Third Post").

It displays name property because it doesn't find any helper for name, and then it searches in the current data context and found property with the same name name and displays that.

Data context in helpers and events

Let's take the same snippet and add a delete button:

<template name="posts">
      {{#each posts}}
        <p>{{name}}</p>
       <button>Delete Post</button>
      {{/each}}
    </template>

It displays like below:

First Post <Delete Post>
Second post <Delete Post>
Third post <Delete Post>
..........
..........

In the events:

Template.posts.events({
  'click button': function(){
     console.log(this)
    Posts.remove({_id: this._id });
  }
})

Here, when you click on any delete button, it will delete respective post.

Here we're using this._id: this means data context.

this will give you the data that the helper takes as input to display.

For example, if you click on the delete button beside First Post, then in the events it will give you following data as this:

{name: "First post", _id: "xxx", ......},

because that is the data context available when it displays that content.

Same if you click on the button beside second post:

{name: "Second post", _id: "yyy", ......},

And same goes with the helpers too.

I hope this helps at least someone out there.

Upvotes: 2

Fred S
Fred S

Reputation: 1

This is not easy to explain. Like you I used it in tutorial without knowing it. After some research I found the best explanation, a visual one. You can have a look at Discover Meteor article about "Templates & Data Contexts". Hope it will clarify your mind about it.

Upvotes: 0

Related Questions