SaveModalLayoutView.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. define([ "require", "backbone", "hbs!tmpl/search/save/SaveModalLayoutView_tmpl", "utils/Utils", "modules/Modal", "utils/UrlLinks", "platform", "models/VSearch", "utils/CommonViewFunction", "utils/Messages" ], function(require, Backbone, SaveModalLayoutViewTmpl, Utils, Modal, UrlLinks, platform, VSearch, CommonViewFunction, Messages) {
  2. var SaveModalLayoutView = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "SaveModalLayoutView",
  4. template: SaveModalLayoutViewTmpl,
  5. regions: {},
  6. ui: {
  7. saveAsName: "[data-id='saveAsName']"
  8. },
  9. templateHelpers: function() {
  10. return {
  11. selectedModel: this.selectedModel ? this.selectedModel.toJSON() : null
  12. };
  13. },
  14. events: function() {
  15. var events = {};
  16. return events;
  17. },
  18. initialize: function(options) {
  19. var that = this;
  20. if (_.extend(this, _.pick(options, "selectedModel", "collection", "getValue", "isBasic", "saveObj")),
  21. this.model = new VSearch(), this.saveObj) this.onCreateButton(); else {
  22. var modal = new Modal({
  23. title: (this.selectedModel ? "修改" : "创建") + " 你的常用搜索 " + (this.selectedModel ? "" : ""),
  24. content: this,
  25. cancelText: "Cancel",
  26. okCloses: !1,
  27. okText: this.selectedModel ? "修改" : "创建",
  28. allowCancel: !0
  29. }).open();
  30. modal.$el.find("button.ok").attr("disabled", "true"), modal.on("ok", function() {
  31. modal.$el.find("button.ok").attr("disabled", "true"), that.onCreateButton(modal);
  32. }), modal.on("closeModal", function() {
  33. modal.trigger("cancel");
  34. });
  35. }
  36. },
  37. onCreateButton: function(modal) {
  38. var that = this, obj = {
  39. name: this.ui.saveAsName.val ? this.ui.saveAsName.val() : null
  40. };
  41. if (this.selectedModel) {
  42. var saveObj = this.selectedModel.toJSON();
  43. saveObj.name = obj.name;
  44. } else {
  45. obj.value = this.getValue(), this.saveObj && _.extend(obj, this.saveObj);
  46. var saveObj = CommonViewFunction.generateObjectForSaveSearchApi(obj);
  47. this.isBasic ? saveObj.searchType = "BASIC" : saveObj.searchType = "ADVANCED";
  48. }
  49. this.model.urlRoot = UrlLinks.saveSearchApiUrl(), this.model.save(saveObj, {
  50. type: saveObj.guid ? "PUT" : "POST",
  51. success: function(model, data) {
  52. if (that.collection) if (saveObj.guid) {
  53. var collectionRef = that.collection.find({
  54. guid: data.guid
  55. });
  56. collectionRef && collectionRef.set(data), Utils.notifySuccess({
  57. content: obj.name + Messages.getAbbreviationMsg(!1, "editSuccessMessage")
  58. });
  59. } else that.collection.add(data), Utils.notifySuccess({
  60. content: obj.name + Messages.getAbbreviationMsg(!1, "addSuccessMessage")
  61. });
  62. }
  63. }), modal && modal.trigger("cancel");
  64. }
  65. });
  66. return SaveModalLayoutView;
  67. });