application/js/content.js in spontaneous-0.2.0.alpha2 vs application/js/content.js in spontaneous-0.2.0.alpha3

- old
+ new

@@ -14,10 +14,11 @@ return (this.container ? this.container.uid() : '') + '/' + this.content.id; }, id: function() { return this.content.id; }, + is_page: function() { return this.type().is_page(); }, target: function() { @@ -60,60 +61,54 @@ list.push(fields[names[i]]); } return list; }.cache(), + // fields that should be listed in the main field column text_fields: function() { - var fields = [], all_fields = this.field_list(); - for (var i = 0, ii = all_fields.length; i < ii; i++) { - var f = all_fields[i]; - if (!f.is_image()) { - fields.push(f); - } - } - return fields; + return this.filter_fields(function(f) { return !f.is_image(); }); }, + // fields that should be saved as strings + string_fields: function() { + return this.filter_fields(function(f) { return !f.is_file(); }); + }, image_fields: function() { - var fields = [], all_fields = this.field_list(); - for (var i = 0, ii = all_fields.length; i < ii; i++) { - var f = all_fields[i]; - if (f.is_image()) { - fields.push(f); - } - } - return fields; + return this.filter_fields(function(f) { return f.is_image(); }); }, file_fields: function() { + return this.filter_fields(function(f) { return f.is_file(); }); + }, + filter_fields: function(filter) { var fields = [], all_fields = this.field_list(); for (var i = 0, ii = all_fields.length; i < ii; i++) { var f = all_fields[i]; - if (f.is_file()) { - fields.push(f); - } + if (filter(f)) { fields.push(f); } } return fields; }, fields: function() { var fields = {}, type = this.type(), prototypes = type.field_prototypes; for (var i = 0, ii = this.content.fields.length; i < ii; i++) { - var f = this.content.fields[i], - prototype = prototypes[f.name], - type_class = this.constantize(prototype.type) - if (!type_class) { - console.warn( - "Content#fields:", - "Field has invalid type", prototype.type, - "content_id:", this.content.id, - "type:", "'"+type.title+"'", - "field_name:", f.name - ); - type_class = Spontaneous.FieldTypes.StringField; + var f = this.content.fields[i], prototype, type_class; + prototype = prototypes[f.name]; + if (f && prototype) { + type_class = this.constantize(prototype.type); + if (!type_class) { + console.warn( + "Content#fields:", + "Field has invalid type", prototype.type, + "content_id:", this.content.id, + "type:", "'"+type.title+"'", + "field_name:", f.name + ); + type_class = Spontaneous.FieldTypes.StringField; + } + var field = new type_class(this, f); + // field.watch('value', this.field_updated.bind(this, field)); + fields[f.name] = field; } - var field = new type_class(this, f); - // field.watch('value', this.field_updated.bind(this, field)); - fields[f.name] = field; }; return fields; }.cache(), field: function(name) { @@ -136,11 +131,11 @@ save_complete: function(response) { if (response) { var fields = response.fields; for (var i = 0, ii = fields.length; i < ii; i++) { var values = fields[i], field = this.field(values.name); - field.update(values); + if (field) { field.update(values); } } } }, has_fields: function() { @@ -206,11 +201,10 @@ }, toggle_visibility: function() { Spontaneous.Ajax.post(['/toggle', this.content.id].join('/'), {}, this.visibility_toggled.bind(this)); }, visibility_toggled: function(result) { - console.log('visibility_toggled', result) this.set('hidden', result.hidden); // this.trigger('visibility_toggled', result); }, destroyed: function() { var page = S.Editing.get('page'); @@ -222,9 +216,18 @@ this.repositioned(); }.bind(this)); }, repositioned: function() { this.trigger('repositioned'); + }, + state: function() { + return S.State.get(this); + }, + setFieldMetadata: function(field, key, value) { + this.state().setFieldMetadata(field, key, value); + }, + getFieldMetadata: function(field, key, value) { + return this.state().getFieldMetadata(field, key); } }); return Content; }(jQuery, Spontaneous));