Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

List view doesnt refresh after added a data

$
0
0
Hello everyone,

since 3 days im fighting with CanJs to update my list of datas after added a data.
basically i have 3 moustache views.
one for app view with inside 2 other views (list and create)

when i destroy data is removed of the database and list is refreshed.
but when i submit a new data with the form, data is saved but view is not refresh.
the event {Client} created is never triggered.

what have i forgot?



the coffeescript file
  1. Client = can.Model.extend
  2. findAll : 'GET /api/clients'
  3. findOne : 'GET /api/clients/{id}'
  4. create : 'POST /api/clients'
  5. update : 'PUT /api/clients/{id}'
  6. destroy : 'DELETE /api/clients/{id}'
  7. , {}

  8. Client.List = can.Model.List.extend
  9. Map: Client
  10. , filter: (tester) ->
  11. clients = new Client.List()
  12. this.each (client) ->
  13. if tester(client)
  14. clients.push(client)
  15. return clients
  16. can.Component.extend
  17. tag: 'clients-create'
  18. template: can.view("clients-create-template")
  19. scope:
  20. submitClient: (scope, el, ev) ->
  21. ev.preventDefault()
  22. client = 
  23. name: this.attr('name')
  24. notes: this.attr('notes')
  25. new Client client
  26. .save()
  27. this.attr('name', '')
  28. this.attr('notes', '')
  29. console.log(Client.List(client))

  30. can.Component.extend
  31. tag: 'tbody'
  32. template: can.view("clients-list-template")
  33. scope:
  34. edit: (client) ->
  35. client.attr("editing", true)
  36. updateTodo: (client, el) ->
  37. client.attr("name", el.val())
  38. client.attr("notes", el.val())
  39. client.attr("editing",false)
  40. events: 
  41. "{Client} created": (construct, ev, newClient) ->
  42. console.log(newClient)
  43. this.scope.attr('clients').push(newClient)

  44. can.Component.extend
  45. tag: 'clients-app'
  46. scope:
  47. Client: Client
  48. clients: new Client.List({})
  49. displayedClients: () ->
  50. return this.attr('clients')


  51. # make sure to initialize the Control
  52. # new Routing(document);
  53. # can.route.ready()

  54. frag = can.view("clients-app-template",{})

  55. $('#clients').html(frag)
the app view
  1. <script type="text/mustache" id="clients-app-template">
  2. <clients-app>
  3. <a can-click="create" class="button secondary popup-link" href="#!create"><i class="fa fa-plus-circle"></i> Add Client</a>
  4. <clients-create></clients-create>
  5. @{{#if clients}}
  6. <table>
  7. <thead>
  8. <tr>
  9. <th>Id</th>
  10. <th>Name</th>
  11. <th>Has projects</th>
  12. <th>Notes</th>
  13. <th>Actions</th>
  14. </tr>
  15. </thead>
  16. <tbody clients='displayedClients'></tbody>
  17. </table>
  18. @{{else}}
  19. <h3>No data found in database.</h3>
  20. @{{/clients}}
  21. </clients-app>
  22. </script>
the list view
  1. <script type="text/mustache" id="clients-list-template">
  2. @{{#each clients}}
  3.     <tr class="client">
  4. <td data-title="Id">@{{id}}</td>
  5. <td data-title="Name">@{{name}}</td>
  6. <td data-title="Has projects">@{{#if projects}} Yes @{{else}} No @{{/projects}}</td>
  7. <td data-title="Notes">@{{#if notes}} Yes @{{else}} No @{{/notes}}</td>
  8. <td data-title="Actions">
  9. <a href=""><i class="fa fa-edit"></i></a> edit
  10. <a can-click="destroy"><i class="fa fa-minus-square-o"></i></a> delete
  11. </td>
  12. </tr>
  13. @{{/each}}
  14. </script>
the create form view
  1. <script type="text/mustache" id="clients-create-template">
  2. <form method="POST" action="http://iprofero.local/admin/clients" accept-charset="UTF-8" can-submit="submitClient">
  3. <input name="_token" type="hidden" value="d5KDyzm5plECCdhGKdM3fyMrbOmjRxxyMuH8m0In">
  4. <div class="row">
  5. <label for="name">Enter client name</label> <input required="1" can-value="name" name="name" type="text" id="name">
  6. </div>
  7. <div class="row">
  8. <label for="notes">Add a note</label> <textarea can-value="notes" name="notes" cols="50" rows="10" id="notes"></textarea></div>
  9. <input class="create primary" type="submit" value="Create">
  10. </form>
  11. </script>


Viewing all articles
Browse latest Browse all 3491

Trending Articles