mercredi 19 août 2015

FindRecord with Ember and MongoDB

I have been looking on the web for an answer to this but I have not been able to find one, maybe I am not asking the right questions but here it goes.

So Im building a CRUD webapp using express, mongodb and ember cli, so far I can save data to my mongodb database (/events/new) and display my events (events/all) however I seem to be stuck when accessing an individual event my ideal URL will be /events/:eventname...

I know i'm getting the right result from the server, and I am getting ONLY ONE, however in the ember console, my model shows all my records, plus I cant seem to render the actual content from my model to the screen. IF I happen to go localhost:300/event/XXXX my ember console shows two records the one I want and some sort of dummy one having XXXX as the ID and the rest of the fields are undefined... Do i need to create a new route to look for the single record? or is there a way to use the same route I defined to look for all my records in events/all ? How do i get the data displayed on the screen ? Why do i see all my records when I access /events/eventname ? Am I following best practices here ?

Express:

      app.get('/api/events', function(req,res) {
    eventsModel.find({},function(err,docs) {
        if(err) {
            res.send({error:err});
        }
        else {
        // we only want to display the current events,
        // so we filter throught them and return
        // only the current ones
        var current = [];
        var today   = new Date();
        var tomorrow = today.setDate(today.getDate() - 1);

        docs.forEach(function(item){
          if(item.date >= tomorrow){
            // console.log('new');
            current.push(item);
          }
        });
        res.send({events:current});
        console.log('/api/events');
        }
    });
  });

  app.get('/api/events/:name', function(req, res){
    console.log(req.url);
    console.log('/api/events/event');
    eventsModel.findOne({name: req.params.name},function(err,docs) {
      if(err) {
            res.send({error:err});
        }
      else{
        res.send({event:docs});
      }
    });
  });

Ember Router.js

    Router.map(function() {
  this.route('events', function() {
    this.route('new');
    this.route('update');
    this.route('all');
    this.route('event', { path: ':name' });
  });
});

Ember events.event Routes

    export default Ember.Route.extend({
  model: function(params) {
    this.store.find('event', params.name);
  }
});

Ember events.all Template

    {{#if model}}
  {{#each model as |event| }}
    <h2>{{#link-to 'events.event' event.name}}{{event.name}}{{/link-to}}</h2>
    <p>{{event.date}}</p>
    <p>{{event.location}}</p>
    <p>{{event.address}}</p>
    <a href={{event.url}}>Read More</a>
  {{/each}}
{{else}}

Ember events.event Template

    {{#each model as |item|}}
  {{item.name}}
{{/each}}
{{outlet}}

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire