perak / kitchen-examples

Meteor Kitchen examples
164 stars 115 forks source link

show customer name rather than id #32

Open cyclops24 opened 8 years ago

cyclops24 commented 8 years ago

Hi @perak , I have a simple question. In the example-invoices how do transform and show customer name in rather than id the DataView?? I talk about this page. I read json file a lot but I don't understand. You have this two field in collection definition but how to fill customer.name:

{
                        "name": "customerId",
                        "title": "Customer",
                        "required": true,
                        "input": "select",
                        "lookup_query_name": "customer_list",
                        "lookup_field": "name",
                        "lookup_key": "_id",
                        "show_in_dataview": false,
                        "show_in_read_only_form": false,
                        "exportable": false,

                        "join_collection": "customers",
                        "join_fields": ["name"],
                        "join_container": "customer"
                    },

                    {
                        "name": "customer.name",
                        "title": "Customer",
                        "show_in_insert_form": false,
                        "show_in_update_form": false,
                        "exportable": true
                    },
perak commented 8 years ago

@cyclops24 customer.name is automatically filled with join described in previous customerId field.

These three lines of code:

"join_collection": "customers",
"join_fields": ["name"],
"join_container": "customer"

join_collection - from which (foreign) collection we want to join data into this collection? join_fields: - which fields we want to fetch from foreign collection? join_container: - field name in which data will be fetched from foreign collection, in this case customer - you can name it as you wish.

Now, result from this is that Invoices.find/findOne will return something like this:

{
  ...normal fields from Invoices collection...
  "customer": {
    "name": "Bruce Lee"
  }
}

If you change join rules to this:

{
  "join_collection": "customers",
  "join_fields": ["name", "email"],
  "join_container": "foo"
}

Results will be:

{
  ...normal fields from Invoices collection...
  "foo": {
    "name": "Bruce Lee",
    "email": "bruce@hongkong.com"
  }
}

This is automatically performed by perak:joins package. Look at docs to see how to perform the same thing manually without kitchen.

cyclops24 commented 8 years ago

Thanks @perak , I try to add this feature to my project and after some try find this small bug: when you enable collection2 support project not compile and crashed with this error message: Unexpected token and after remove "use_collection2": true, all things worked well. If need I can post it as new issue in kitchen-website repository.