나는 다음과 같은보기가이 같은backbone.js에서 뷰를 다시 렌더링하는 것을 처리하는 가장 좋은 방법은 무엇입니까?
Main.Views.Login = EventQ.View.extend({
events: {
"submit form": "login"
},
template: "login",
login: function(e) {
var me = this;
$.ajax({
url: "/api/users/login",
type: 'POST',
dataType: "json",
data: $(e.currentTarget).serializeArray(),
success: function(data, status){
EventQ.app.router.navigate('dashboard', true);
},
error: function(xhr, status, e) {
var result = $.parseJSON(xhr.responseText);
me.render_with_errors(result.errors);
}
});
return false;
},
render: function(done) {
var me = this;
// Fetch the template, render it to the View element and call done.
EventQ.fetchTemplate(me.template, function(tmpl) {
me.el.innerHTML = tmpl(me.model.toJSON());
done(me.el);
});
},
render_with_errors: function(errors) {
var me = this;
// Fetch the template, render it to the View element and call done.
EventQ.fetchTemplate(this.template, function(tmpl) {
me.el.innerHTML = tmpl(errors);
});
}
});
간단한 템플릿 : 내가 무엇을 찾고 있어요 것은 오류가 반환하는 경우 템플릿을 다시 렌더링 할 수 있습니다
<form>
<input name="username" />
<input name="password" />
<button type="submit" />
</form>
하지만, 입력이 채워진 채로 유지하십시오. 오류 템플릿은 다음과 같습니다.
<form>
<input name="username" />
<label class="error">required</label>
<input name="password" />
<button type="submit" />
</form>
확인할 수있는 모델 또는 뷰에보기를 바인딩하는 방법이 있습니까? 현재 render_with_errors는 폼에 채워진 모든 데이터를 잃어버린 사실을 제외하면 작동합니다.
오류에 대해 별도의 템플릿을 사용하지 않았습니까? 어쩌면 실제 상황에서 템플릿이 더 다른 것일 수 있습니다. 그러나 귀하의 예제에서 오류/성공 처리기에 오류 메시지를 표시하거나 숨길 수 있습니다. 또는 온 - 더 - 플라이 검증으로도. –