Last active
December 16, 2015 19:59
-
-
Save pdjota/5488793 to your computer and use it in GitHub Desktop.
Some backbone changes for using empty models with Razor views.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint indent:2, nomen:false, browser:true */ | |
/*globals define, console, ActiveXObject */ | |
/** | |
* Html custom sync for html usage. | |
*/ | |
define(['underscore', 'backbone'], function (_, Backbone) { | |
'use strict'; | |
var HtmlSync = {}; | |
var methodMap = { | |
'create': 'POST', | |
'update': 'PUT', | |
'patch': 'PATCH', | |
'delete': 'DELETE', | |
'read': 'GET' | |
}; | |
var urlError = function() { | |
throw new Error('A "url" property or function must be specified'); | |
}; | |
HtmlSync.sync = function (method, model, options) { | |
console.log('syncing...', model.emptyModel); | |
var type = methodMap[method]; | |
// Default options, unless specified. | |
_.defaults(options || (options = {}), { | |
emulateHTTP: Backbone.emulateHTTP, | |
emulateJSON: Backbone.emulateJSON | |
}); | |
if (model.emptyModel) { | |
var params = {type: type, dataType: 'html'}; | |
} else { | |
// Default JSON-request options. | |
var params = {type: type, dataType: 'json'}; | |
} | |
// Ensure that we have a URL. | |
if (!options.url) { | |
params.url = _.result(model, 'url') || urlError(); | |
} | |
// Ensure that we have the appropriate request data. | |
if (options.data && model && (method === 'create' || method === 'update' || method === 'patch')) { | |
params.contentType = 'application/json'; | |
params.data = JSON.stringify(options.attrs || model.toJSON(options)); | |
} | |
// For older servers, emulate JSON by encoding the request into an HTML-form. | |
if (options.emulateJSON) { | |
params.contentType = 'application/x-www-form-urlencoded'; | |
params.data = params.data ? {model: params.data} : {}; | |
} | |
// For older servers, emulate HTTP by mimicking the HTTP method with `_method` | |
// And an `X-HTTP-Method-Override` header. | |
if (options.emulateHTTP && (type === 'PUT' || type === 'DELETE' || type === 'PATCH')) { | |
params.type = 'POST'; | |
if (options.emulateJSON) params.data._method = type; | |
var beforeSend = options.beforeSend; | |
options.beforeSend = function (xhr) { | |
xhr.setRequestHeader('X-HTTP-Method-Override', type); | |
if (beforeSend) return beforeSend.apply(this, arguments); | |
}; | |
} | |
// Don't process data on a non-GET request. | |
if (params.type !== 'GET' && !options.emulateJSON) { | |
params.processData = false; | |
} | |
// If we're sending a `PATCH` request, and we're in an old Internet Explorer | |
// that still has ActiveX enabled by default, override jQuery to use that | |
// for XHR instead. Remove this line when jQuery supports `PATCH` on IE8. | |
if (params.type === 'PATCH' && window.ActiveXObject && | |
!(window.external && window.external.msActiveXFilteringEnabled)) { | |
params.xhr = function () { | |
return new ActiveXObject("Microsoft.XMLHTTP"); | |
}; | |
} | |
// Make the request, allowing the user to override any Ajax options. | |
var xhr = options.xhr = Backbone.ajax(_.extend(params, options)); | |
model.trigger('request', model, xhr, options); | |
return xhr; | |
}; | |
HtmlSync.parse = function (resp) { | |
if (this.emptyModel) { | |
this.set('html',resp); | |
} else { | |
return resp; | |
} | |
}; | |
return HtmlSync; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'jquery', | |
'underscore', | |
'backbone', | |
'htmlSync' | |
], function($, _, Backbone, HtmlSync) { | |
var Projects = Backbone.Model.extend({ | |
url: '/Home/Projects', | |
emptyModel: true, | |
parse: HtmlSync.parse, | |
sync: HtmlSync.sync | |
}); | |
return Projects; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'jquery', | |
'backbone', | |
'models/projects' | |
], function ($, Backbone, Projects) { | |
var ProjectsView = Backbone.View.extend({ | |
el: $('#projects_panel'), | |
initialize: function () { | |
var projects = new Projects(); | |
this.model = projects; | |
this.listenTo(projects, 'sync', this.render); | |
this.model.fetch(); | |
}, | |
render: function () { | |
var html = this.model.get('html'); | |
this.$el.html(html); | |
} | |
}); | |
return ProjectsView; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment