Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this._id is not accessible to controller. How to get the id? #50

Open
ccfiel opened this issue Mar 3, 2015 · 3 comments
Open

this._id is not accessible to controller. How to get the id? #50

ccfiel opened this issue Mar 3, 2015 · 3 comments

Comments

@ccfiel
Copy link

ccfiel commented Mar 3, 2015

I have this in my dashboard.html

      {{#each todos}}
        <li class="list-group-item">
            {{ task }}
            <button class="delete"> Delete</button>
        </li>
      {{/each}}

and in the controller dashboard.js I have this

    DashboardController.events({
      'click .delete': function () {
        Meteor.call('ToDos.delete',this._id);
      }
    });

In a simple boilerplate I can access the id of the collection in the event using this._id but with this setup it has a null value. Any ideas how to get the id of the collection todo in the controller?

@ovidb
Copy link

ovidb commented Apr 9, 2015

I think the problem is with your code, you should have something like this:

   {{#each todos}}
         {{> task }}
   {{/each}}

then have a template like this one:

  <template name="task">
      <button class="delete"></button>
  </template>

Then you could reference the task by id from inside the template like this:

   Template.task.events({
      "click .delete": function () {
         Meteor.call("ToDos.delete", this._id);
      },
   });

@ramstein74
Copy link

You are suggesting to add events to the template. That is a winner answer.

The problem is that when you forget Template.x.events and use Controller.events
the _id is not available.

How do we access the _id inside a controller.event ?

@ovidb
Copy link

ovidb commented Apr 22, 2015

You should read more about RouteController and you should be more aware of your context.

Anyways, try something along these lines:

  {{#each todos}}
    <li class="list-group-item">
        {{ task }}
        <button id={{_id}} class="delete"> Delete</button>
    </li>
  {{/each}}

and this in your controller (notice the event, and template parameters. ):

  DashboardController.events({
    'click .delete': function (event, template) {
       Meteor.call("ToDos.delete", event.currentTarget.id);
    }
  });

I'll have to point out that this isn't the quite optimal but maybe you can figure out a cleverer way of doing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants