Meteor-Community-Packages / meteor-autocomplete

Client/server autocompletion designed for Meteor's collections and reactivity.
https://atmospherejs.com/mizzao/autocomplete
MIT License
350 stars 109 forks source link

If no match found, add a button to create new item in collection #105

Open taxigy opened 8 years ago

taxigy commented 8 years ago

Let's say I have a simple collection:

Contact = new SimpleSchema({
    name: {
        type: String,
        unique: true
    }
});
Contacts = new Mongo.Collection('contacts');

And I want to let app users add new items into this collection either in insert or update AutoForm. In current state, it seems not possible, and values that don't match are just ignored.

mizzao commented 8 years ago

Yep, this is already mentioned in #95 and also I'd like to add AutoForm support at some point (#9, #91).

Maybe you can write a PR?

MeKarina commented 8 years ago

I think we should keep this package lightweight...

Here what i did if you need to create new item if no-match found:

on submit event
var i = t.find('input').value   // retrieve input value from autocomplete input box
console.log(i); // return user's input value
var c = C.find({}, {    // fetch array of collection
    fields: {title:1, src:1, sC:1, _id:0},
    transform: null
}).fetch();
console.log(c);
var a = _.filter(c, _.matches({'title':i}));    // filter c to only retrieve matched object
console.log(a); // return array of matched objects
if (_.isEmpty(a)) { // if a is empty (no-match) then insert i into collection C. The newly created documents will show at auto suggestion after submit event. there is no 'no-match' again after this
  return C.insert
} else {    // if a not empty then add i into list
    _.forEach(a, function(x) {
    // Code adding i into collection list. List.insert...
  });
}

thankYou...