Created
February 12, 2016 18:26
-
-
Save arturaugusto/2bcec1edd1423ec6c937 to your computer and use it in GitHub Desktop.
autoValue problem solved on remove array item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <head> | |
| <title>teste</title> | |
| </head> | |
| <body> | |
| {{> insertBookForm}} | |
| {{> removeItem}} | |
| </body> | |
| <template name="hello"> | |
| <button>Click Me</button> | |
| <p>You've pressed the button {{counter}} times.</p> | |
| </template> | |
| <template name="removeItem"> | |
| <table> | |
| {{#if books}} | |
| {{#each books}} | |
| {{#each chapters}} | |
| <tr> | |
| <td>{{name}}</td> | |
| <td>{{pages}}</td> | |
| <td style="width: 70px"><a href="#" name="{{id}}" class="chap-delete-btn"> x </a></td> | |
| </tr> | |
| {{/each}} | |
| {{/each}} | |
| {{/if}} | |
| </table> | |
| </template> | |
| <template name="insertBookForm"> | |
| {{> quickForm collection="Books" id="insertBookForm" type="insert"}} | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Books = new Mongo.Collection("books"); | |
| Books.attachSchema(new SimpleSchema({ | |
| title: { | |
| type: String, | |
| label: "Title", | |
| max: 200 | |
| }, | |
| author: { | |
| type: String, | |
| label: "Author" | |
| }, | |
| copies: { | |
| type: Number, | |
| label: "Number of copies", | |
| min: 0 | |
| }, | |
| lastCheckedOut: { | |
| type: Date, | |
| label: "Last date this book was checked out", | |
| optional: true | |
| }, | |
| summary: { | |
| type: String, | |
| label: "Brief summary", | |
| optional: true, | |
| max: 1000 | |
| }, | |
| chapters: { | |
| type: Array, | |
| optional: true | |
| }, | |
| 'chapters.$': { | |
| type: Object, | |
| optional: true | |
| }, | |
| 'chapters.$._id': { | |
| type: String, | |
| optional: true, | |
| autoValue: function(){ | |
| if (!this.isSet && this.operator !== "$pull") { // this check here is necessary! | |
| return Random.id(); | |
| } | |
| } | |
| }, | |
| 'chapters.$.name': { | |
| type: String, | |
| optional: true | |
| }, | |
| 'chapters.$.pages': { | |
| type: Number, | |
| optional: true | |
| } | |
| })); | |
| if (Meteor.isClient) { | |
| // counter starts at 0 | |
| Session.setDefault('counter', 0); | |
| Meteor.subscribe("books"); | |
| Template.hello.helpers({ | |
| counter: function () { | |
| return Session.get('counter'); | |
| } | |
| }); | |
| Template.hello.events({ | |
| 'click button': function () { | |
| // increment the counter when button is clicked | |
| Session.set('counter', Session.get('counter') + 1); | |
| } | |
| }); | |
| Template.removeItem.helpers({ | |
| books: function () { | |
| return Books.find(); | |
| } | |
| }); | |
| Template.removeItem.events({ | |
| 'click .chap-delete-btn': function(e) { | |
| e.preventDefault(); | |
| var chap = this; | |
| var id = e.currentTarget.name; | |
| Meteor.call('removeChap', id, chap); | |
| } | |
| }); | |
| } | |
| if (Meteor.isServer) { | |
| Meteor.startup(function () { | |
| Meteor.publish("books", function () { | |
| return Books.find(); | |
| }); | |
| Meteor.methods({ | |
| removeChap: function(id, chap){ | |
| console.log(chap); | |
| //Books.update( {_id: '2JkRGCcAvHQWuhYCT'} , {$pull : {chapters : chap} } ); | |
| Books.update({_id:id}, {$pull : {chapters : {_id:chap._id}}}); | |
| } | |
| }); | |
| // code to run on server at startup | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment