Wednesday, 7 January 2015

array field of keystonejs or sub fields of keystonejs

what i want is a Dynamic array field, which like tags for a blog post.
However keystonejs doesn't support it.
I have a temporary solution, I would like to share:
Design.add( {
design: { type: String , initial: true, required: true, index: true },
priceGroup: { type: String },
price: { type: String},
image: { type: Types.CloudinaryImage },
},
"Colours", { 
colours:{
1: { 
type: String,collapse:true
},
2: { 
type: String,collapse:true
},
3: { 
type: String,collapse:true
},
4: { 
type: String,collapse:true
},
5: { 
type: String,collapse:true
},
6: { 
type: String,collapse:true
},
7: { 
type: String,collapse:true
},
8: { 
type: String,collapse:true
},
9: { 
type: String,collapse:true
},
10: { 
type: String,collapse:true
},
11: { 
type: String,collapse:true
},
12: { 
type: String,collapse:true
},
13: { 
type: String,collapse:true
},
14: { 
type: String,collapse:true
},
15: { 
type: String,collapse:true
},
16: { 
type: String,collapse:true
}
}
}
);

I know I am terrible smart!!!!!   :-)

All colours fields are collapsed when you open the create view of admin UI. 

if you need only one colour open the first colour field.

All looks great!!, But it will always have 16 sub items in mongodb like this:

{
    "_id" : ObjectId("54ad54c2825783d46abe749b"),
    "design" : "aaa",
    "__v" : 0,
    "colours" : {
        "1" : "red",
        "10" : "",
        "11" : "",
        "12" : "",
        "13" : "",
        "14" : "",
        "15" : "",
        "16" : "",
        "2" : "blue",
        "3" : "",
        "4" : "",
        "5" : "",
        "6" : "",
        "7" : "",
        "8" : "",
        "9" : ""
    },
    "price" : "xxxxxx",
    "priceGroup" : "ddd"
}



No comments:

Post a comment