我是由Vue.js制作电子邮件杂志表格。一个视图中有四种独立的形式(html)。对于表单是相同的。(Implememts是相同的。)但每个表单使用diefferent数据,所以我不得不使ViewModel四次,更改el
。我认为它有点冗长,我不想让ViewModel四次,我只想要它一次。有没有人知道简化这个实现有什么好主意?谢谢。
class @Article.EmailMagazine
constructor: () ->
render: (tag)->
new Vue
el: "#email_magazine_registration_" + tag,
data:
registration: true
email: null
errors:
email: false
messages: []
methods:
build_refresh: ->
@errors.email = false
@messages = []
validate_before: ->
...
validate_after: (message)->
...
cancel: ->
@registration = false
save: ->
@validate_before()
return if @errors.email
$.ajax
async: true
type: 'POST'
dataType: 'json'
url: '/user_email_magazines'
data:
email: @email
context: this
success: (data) ->
@registration = false
error: (data) ->
@validate_after(data.responseJSON[0])
$ ->
if $('body').hasClass('articles')
top = new Article.EmailMagazine()
top.render('top')
nav = new Article.EmailMagazine()
nav.render('nav')
side = new Article.EmailMagazine()
side.render('side')
under = new Article.EmailMagazine()
under.render('under')