|
28 | 28 | render: function () { |
29 | 29 | var slug = this.model ? this.model.get('slug') : '', |
30 | 30 | pubDate = this.model ? this.model.get('published_at') : 'Not Published', |
31 | | - $pubDateEl = this.$('.post-setting-date'); |
| 31 | + $pubDateEl = this.$('.post-setting-date'), |
| 32 | + $postSettingSlugEl = this.$('.post-setting-slug'); |
32 | 33 |
|
33 | | - $('.post-setting-slug').val(slug); |
| 34 | + $postSettingSlugEl.val(slug); |
34 | 35 |
|
35 | 36 | // Update page status test if already a page. |
36 | 37 | if (this.model && this.model.get('page')) { |
|
47 | 48 | this.$('.delete').removeClass('hidden'); |
48 | 49 | } |
49 | 50 |
|
| 51 | + // Apply different style for model's that aren't |
| 52 | + // yet persisted to the server. |
| 53 | + // Mostly we're hiding the delete post UI |
| 54 | + if (this.model.id === undefined) { |
| 55 | + this.$el.addClass('unsaved'); |
| 56 | + } else { |
| 57 | + this.$el.removeClass('unsaved'); |
| 58 | + } |
| 59 | + |
50 | 60 | $pubDateEl.val(pubDate); |
51 | 61 | }, |
52 | 62 |
|
53 | 63 | // Requests a new slug when the title was changed |
54 | 64 | updateSlugPlaceholder: function () { |
55 | | - var title = this.model.get('title'); |
56 | | - |
57 | | - $.ajax({ |
58 | | - url: Ghost.paths.apiRoot + '/posts/getSlug/' + encodeURIComponent(title), |
59 | | - success: function (result){ |
60 | | - // ToDo: Find better selector |
61 | | - $('.post-setting-slug')[0].placeholder = result; |
62 | | - } |
63 | | - }); |
| 65 | + var title = this.model.get('title'), |
| 66 | + $postSettingSlugEl = this.$('.post-setting-slug'); |
| 67 | + |
| 68 | + // If there's a title present we want to |
| 69 | + // validate it against existing slugs in the db |
| 70 | + // and then update the placeholder value. |
| 71 | + if (title) { |
| 72 | + $.ajax({ |
| 73 | + url: Ghost.paths.apiRoot + '/posts/getSlug/' + encodeURIComponent(title) + '/', |
| 74 | + success: function (result) { |
| 75 | + $postSettingSlugEl.attr('placeholder', result); |
| 76 | + } |
| 77 | + }); |
| 78 | + } else { |
| 79 | + // If there's no title set placeholder to blank |
| 80 | + // and don't make an ajax request to server |
| 81 | + // for a proper slug (as there won't be any). |
| 82 | + $postSettingSlugEl.attr('placeholder', ''); |
| 83 | + return; |
| 84 | + } |
64 | 85 | }, |
65 | 86 |
|
66 | 87 | selectSlug: function (e) { |
|
74 | 95 | slugEl = e.currentTarget, |
75 | 96 | newSlug = slugEl.value; |
76 | 97 |
|
| 98 | + // If the model doesn't currently |
| 99 | + // exist on the server (aka has no id) |
| 100 | + // then just update the model's value |
| 101 | + if (self.model.id === undefined) { |
| 102 | + this.model.set({ |
| 103 | + slug: newSlug |
| 104 | + }); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
77 | 108 | // Ignore unchanged slugs |
78 | 109 | if (slug === newSlug) { |
79 | 110 | slugEl.value = slug === undefined ? '' : slug; |
|
116 | 147 | pubDateMoment, |
117 | 148 | newPubDateMoment; |
118 | 149 |
|
| 150 | + // if there is no new pub date do nothing |
| 151 | + if (!newPubDate) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
119 | 155 | // Check for missing time stamp on new data |
120 | 156 | // If no time specified, add a 12:00 |
121 | 157 | if (newPubDate && !newPubDate.slice(-5).match(/\d+:\d\d/)) { |
|
164 | 200 | return; |
165 | 201 | } |
166 | 202 |
|
| 203 | + // If the model doesn't currently |
| 204 | + // exist on the server (aka has no id) |
| 205 | + // then just update the model's value |
| 206 | + if (self.model.id === undefined) { |
| 207 | + this.model.set({ |
| 208 | + published_at: newPubDateMoment.toDate() |
| 209 | + }); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
167 | 213 | // Save new 'Published' date |
168 | 214 | this.model.save({ |
169 | 215 | published_at: newPubDateMoment.toDate() |
|
192 | 238 | var pageEl = $(e.currentTarget), |
193 | 239 | page = pageEl.prop('checked'); |
194 | 240 |
|
| 241 | + // Don't try to save |
| 242 | + // if the model doesn't currently |
| 243 | + // exist on the server |
| 244 | + if (this.model.id === undefined) { |
| 245 | + this.model.set({ |
| 246 | + page: page |
| 247 | + }); |
| 248 | + return; |
| 249 | + } |
| 250 | + |
195 | 251 | this.model.save({ |
196 | 252 | page: page |
197 | 253 | }, { |
|
218 | 274 | deletePost: function (e) { |
219 | 275 | e.preventDefault(); |
220 | 276 | var self = this; |
| 277 | + // You can't delete a post |
| 278 | + // that hasn't yet been saved |
| 279 | + if (this.model.id === undefined) { |
| 280 | + return; |
| 281 | + } |
221 | 282 | this.addSubview(new Ghost.Views.Modal({ |
222 | 283 | model: { |
223 | 284 | options: { |
|
0 commit comments