Skip to content

Instantly share code, notes, and snippets.

@arturaugusto
Created February 12, 2016 18:26
Show Gist options
  • Select an option

  • Save arturaugusto/2bcec1edd1423ec6c937 to your computer and use it in GitHub Desktop.

Select an option

Save arturaugusto/2bcec1edd1423ec6c937 to your computer and use it in GitHub Desktop.
autoValue problem solved on remove array item
<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>
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