diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:39:43 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:39:43 -0800 |
commit | 547c1bb8d9240ff5a531890bda193ce0a17edff1 (patch) | |
tree | c7cd108f04a2607a6e44766d1736d4c189534c56 /addons | |
parent | 8b9d19d5a7212d3e0d493ad6a0570434f9049b43 (diff) |
editing server and reference
Diffstat (limited to 'addons')
-rw-r--r-- | addons/reference/reference.go | 42 |
1 files changed, 16 insertions, 26 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go index 6a46e75..48a9748 100644 --- a/addons/reference/reference.go +++ b/addons/reference/reference.go @@ -1,11 +1,11 @@ package api import ( + "bytes" "encoding/json" "fmt" - "log" + "text/template" - "github.com/bosssauce/ponzu/content" "github.com/bosssauce/ponzu/management/editor" "github.com/bosssauce/ponzu/system/api" ) @@ -20,38 +20,28 @@ type Referenceable interface { // IMPORTANT: // The `fieldName` argument will cause a panic if it is not exactly the string // form of the struct field that this editor input is representing -func Select(fieldName string, p interface{}, attrs map[string]string, contentType string) []byte { - ct, ok := content.Types[contentType] - if !ok { - log.Println("Cannot reference an invalid content type:", contentType) - return nil - } - - // get a handle to the underlying interface type for decoding - t := ct() - +func Select(fieldName string, p interface{}, attrs map[string]string, contentType string, tmpl template.Template) []byte { // decode all content type from db into options map // map["?type=<contentType>&id=<id>"]t.String() options := make(map[string]string) - // jj := db.ContentAll(contentType + "__sorted") // make this an API call - jj := api.ContentAll(contentType) - for i := range jj { - err := json.Unmarshal(jj[i], t) - if err != nil { - log.Println("Error decoding into reference handle:", contentType, err) - } + var data []map[string]interface{} + j := api.ContentAll(contentType) - // make sure it is a content.Identifiable - item, ok := t.(content.Identifiable) - if !ok { - log.Println("Cannot use type", contentType, "as a reference since it does not implement content.Identifiable") + err := json.Unmarshal(j, data) + if err != nil { + return nil + } + + for i := range data { + k := fmt.Sprintf("?type=%s&id=%s", contentType, data[i]["id"].(string)) + v := &bytes.Buffer{} + err := tmpl.Execute(v, data[i]) + if err != nil { return nil } - k := fmt.Sprintf("?type=%s&id=%d", contentType, item.ItemID()) - v := item.String() - options[k] = v + options[k] = v.String() } return editor.Select(fieldName, p, attrs, options) |