如何使用具有不同内容的相同警报(?)

How to use the same alert with different content (?)

快速提问

我正在使用 angular strap 来显示一些警报,每个警报都在不同的变量中,但我想避免这样做,有人告诉我使用 lodash 中的 _.extend,但在这种特殊情况下我应该如何使用它?

var loaderAlert1 = $alert({
    animation: 'fx-bounce-right',
    content: 'Loading content, please wait...',
    container: '.alerts-container',
    type: 'info',
    show: true
  }),
  loaderAlert2 = $alert({
    animation: 'fx-bounce-right',
    content: 'Adding Line to BetSlip',
    container: '.alerts-container',
    type: 'info',
    show: false
  }),
  loaderAlert3 = $alert({
    animation: 'fx-bounce-right',
    content: 'Loading your messages, please wait...',
    container: '.alerts-container',
    type: 'info',
    show: false
  });

如您所见,几乎相同,但在某些情况下,show attr 设置为 true 或 false,content: 也会发生变化。

我调用此警报的方式:loaderAlert1.show()loaderAlert1.hide() 等等...

所以我想做类似

的事情
var loaderAlert = $alert({
    animation: 'fx-bounce-right',
    content: 'Loading content, please wait...',
    container: '.alerts-container',
    type: 'info',
    show: true
  });

loaderAlert = {content: 'loading something else'};

loaderAlert = {content: 'loading something GREAT'};

loaderAlert = {content: 'loading only ROCK N ROLL'};

这里我只声明了一个变量,然后一旦我调用这个变量,我就可以改变content:...但是我不知道如何_.extendhere is the function I mentioned

我认为这应该可以解决问题:

   function customAlert( content ) {
         var loaderAlert = $alert({
            animation: 'fx-bounce-right',
            content: content,
            container: '.alerts-container',
             type: 'info',
            show: true
          });

         return loaderAlert;
    }

Е编辑:

var content = 'loading something else', // choose one of the three choices here
     loaderAlert = customAlert(content);
var loaderAlert = $alert({
    animation: 'fx-bounce-right',
    container: '.alerts-container',
    type: 'info',
    show: true
  });

loaderAlert = _.extend({content: 'loading something else'}, loaderAlert);

_.extend 会帮助你,但是在对 $alert 的调用中重复使用一个对象可能不是一个好主意——你可能会发现你需要返回给定的 content 值稍后。

如果您提供一个新对象作为 _.extend 的第一个参数,您可以为每个警报克隆基本参数:

var base = {
    animation: 'fx-bounce-right',
    content: 'Loading content, please wait...',
    container: '.alerts-container',
    type: 'info',
    show: true
  }

$alert(_.extend({}, base, { content: 'first alert' }));
$alert(_.extend({}, base, { content: 'second alert' }));
$alert(_.extend({}, base, { content: 'third alert' }));