0

This question is similar to my previous one: how to communicate with Rails in Ember but some things have changed. Now I know that a kudo is modelled in the server and has the following fields:

  • value
  • comment

It belongs to a user. The current implementation is as below:

<a href="#" class="btn btn-primary" {{action "addKudo" user}}>add</a>


Sks.ApplicationRoute = Ember.Route.extend
  events:
    addKudo: (user) ->
      console.log event
      self = @
      token = $('meta[name="csrf-token"]').attr 'content'
      currentUserCon = @controllerFor 'currentUser'
      kudosLeft = currentUserCon.get 'kudosLeft'

      showFlash = (type, message) ->
        $("#flash")
          .addClass(type)
          .empty()
          .append(message)
          .show()
          .fadeIn()
          .delay(2000)
          .fadeOut 'slow'

      $.post("/kudos", user_id: user.get("id"), authenticity_token: token)
      .done((data, status) =>
        if kudosLeft > 0
          currentUserCon.decrementKudos 1
          showFlash 'alert-success', 'You\'ve added a kudo!'
        else
          showFlash 'alert-error', 'There\'re no kudos left!'
      )
      .fail (data, status) ->
        showFlash 'alert-error', 'Oops! An error occured!'

But now, I'd like to make use of Ember-Data and send a POST request to /kudos (and I'd like to get rid of the jQuery ajax part). Please have a look:

enter image description here

Having the following model:

Sks.Kudo = DS.Model.extend
  user: DS.belongsTo 'Sks.User'
  value: DS.attr 'number'
  comment: DS.attr 'string'

how can I achieve this? What are the neccessary steps to do this? Could anyone shed some light?

The next question is: where select and text area value should point to (bindings)?

Community
  • 1
  • 1
wryrych
  • 1,755
  • 3
  • 19
  • 31
  • i'd use something along the lines of `transaction = this.get('store').transaction(); transaction.createRecord(Sks.Kudo, {user_id: user.get("id")}); transaction.commit(); transaction = null;` – Finn MacCool Apr 11 '13 at 10:27
  • I know that this is something along those lines but what are the steps? Do I have to create a /kudos route even though I don't want ot go to another route but create a POST from a current one (users, user). Then something should handle creating a kudo object, shouldn't it? I don't really know how to connect the dots. :) – wryrych Apr 11 '13 at 10:50
  • you can run this code from any controller and most likely also from any route. i just don't know if `this.get('store').transaction()` works there. you may have to replace `this` with a call to the route's controller. – Finn MacCool Apr 11 '13 at 11:11
  • yeah, that's right: I creating transaction with a Kudo record and commiting it sends a request to /kudos - I didn't know that :) could you also tell me how to handle promise like in my old example (done, fail)? – wryrych Apr 11 '13 at 18:57
  • sorry, i haven't worked with promises yet. – Finn MacCool Apr 12 '13 at 08:12

0 Answers0