Skip to content

Instantly share code, notes, and snippets.

@walter
Last active December 12, 2015 01:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walter/4694743 to your computer and use it in GitHub Desktop.
Save walter/4694743 to your computer and use it in GitHub Desktop.
Example modal code
# copied and modified from a working app, but there maybe typos in adaptation
# untested in this form
# router
App.Router.map (match) ->
@resource 'posts', ->
@route 'new'
@resource 'post', path: '/:post_id'
# router handler
# see mixins/posts_formable
App.PostsNewRoute = Ember.Route.extend Lm.PostssFormable,
model: ->
App.Post.createRecord()
# mixin, so new and edit can use same template/view
App.PostsFormable = Ember.Mixin.create
renderTemplate: ->
@render 'posts/form', ->
outlet: 'modal'
events:
cancel: (post) ->
post.transaction.rollback()
@transitionTo 'posts'
submit: (post) ->
# TODO: add validation handling
post.get('store').commit()
if post.didCreate
@transitionTo 'post', post
# form view
App.PostsFormView = Em.View.extend
tagName: 'form'
classNames: 'modal fade in form-custom-field-modal'.w()
didInsertElement: ->
@$().modal 'show'
willDestroyElement: ->
@$().modal 'hide'
# templates
# posts.hbs (not post.index)
# ...
# <div class="">
# {{outlet}}
# </div>
# </div>
# {{outlet modal}}
# form template
# <div class="modal-header">
# <div class="button close btn-dismiss" {{action cancel content}}>x</div>
# <h2 class="app-icon-large">Post</h2>
# </div>
# <div class="modal-body">
# your form code
# ...
# </div>
# <div class="modal-footer">
# <a class="btn btn-dark" href="javascript;:" {{action cancel content}}>Cancel</a>
# <button class="btn btn-success" type="submit" {{action submit content}}>Save</button>
# </div>
@akshayrawat
Copy link

On line 30, why not do this:

post.on 'didCreate' , ->
   @transitionTo 'post', post

And similarly handle didUpdate for updates.

@akshayrawat
Copy link

Highlighting field level validation errors from commit() in the modal would be really useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment